generated from willCode2Surf/vue3-vite-express-js-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (45 loc) · 1.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import express from "express";
import bodyParser from "body-parser";
import routes from "./express-routes/index.js";
import http from "http";
import { ConfigurationData } from "./config/index.js";
import { WebSocketServer } from "ws";
import cors from "cors";
const app = express();
const wsServer = new WebSocketServer({
noServer: true,
});
const main = async () => {
wsServer.on("connection", (socket) => {
socket.on("message", (message) => {
console.log("SERVER RECEIVED: socket.on(message)", JSON.parse(message));
// echo the user
socket.send(JSON.stringify({ message: "hello client" }));
});
});
app.use(bodyParser.json());
app.use(cors());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use("/", express.static("dist"));
app.use("/routes", routes);
var server = http.createServer(app);
server.listen(ConfigurationData.server.port);
server.on("upgrade", (request, socket, head) => {
wsServer.handleUpgrade(request, socket, head, (socket) => {
wsServer.emit("connection", socket, request);
});
});
console.info(`Server started on port ${ConfigurationData.server.port}`);
console.info(`http://localhost:${ConfigurationData.server.port}`);
};
// entry
main().catch(console.error);
process.on("uncaughtException", function (err) {
console.info("(¯`·._.·(¯`·._.· MASSIVE ERROR ·._.·´¯)·._.·´¯)");
console.log("exception: " + err);
console.info("(¯`·._.·(¯`·._.· MASSIVE ERROR ·._.·´¯)·._.·´¯)");
});