-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
67 lines (51 loc) · 1.37 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// server imports
import express from "express";
import { createServer as HttpServer } from "http";
// env
import { env } from "process";
// storage imports
import Client from "./Engines/StorageEngine/BasicStore";
// Socket import
import { Server as SocketServer } from "socket.io";
import sharedsession from "express-socket.io-session";
import { session_config } from "./routes/index";
import IoController from "./controllers/IoController";
// router import
import router from "./routes";
// ==================================================================================
const PORT = env.chatAppPort || 8000;
// Express APP
const app = express();
app.use(express.static('client/build/'));
// app.use(router);
app.use('/api', router);
app.get('*', (req, res, next) => {
res.sendFile(__dirname + '/client/build/index.html');
})
// http server
const httpServer = HttpServer(app);
// socket instance
const io = new SocketServer(httpServer, {
cors: {
origin: "http://localhost:3000",
credentials: true,
},
});
io.use(
sharedsession(session_config, {
autoSave: true,
})
);
// websock handlers
io.on("connection", IoController.onConnection);
// start server
(async () => {
try {
await Client.connect();
httpServer.listen(PORT, "localhost", () => {
console.log(`Server Listening on ${PORT}`);
});
} catch (err) {
console.log(error);
}
})();