-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (24 loc) · 878 Bytes
/
server.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
const express = require("express");
const http = require("http");
const fileUpload = require("express-fileupload");
const cors = require("cors");
const fs = require("fs");
const WebSocket = require("ws");
const app = express();
const PORT = process.env.PORT || 8000;
const socketController = require("./controllers/socketController");
app.use(cors());
app.use(fileUpload());
app.use(express.static("public"));
app.use("/", socketController.router);
app.use("/", (_req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
fs.createReadStream("public/index.html").pipe(res);
});
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
wss.on("connection", (socket, req) =>
socketController.manager.onConnection(socket, req)
);
server.listen(PORT, () => console.log(`Server listening on port ${PORT}`));
module.exports = app;