-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (49 loc) · 1.81 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
53
54
55
56
57
const path = require("path");
const express = require("express");
const { setHeaders } = require("./middlewares/setHeaders");
const { errorHandler } = require("./middlewares/errorHandler");
const { connectToDB } = require("./models/setup");
const usersRoutes = require("./routes/users");
const gamesRoutes = require("./routes/games");
const leaguesRoutes = require("./routes/leagues");
const chatsRoutes = require("./routes/chats");
const noticesRoutes = require("./routes/notices");
const { bindSocketsToMainServer } = require("./websockets");
const { createServer } = require("https");
const fs = require('fs');
const { morganLogger } = require("./middlewares/morganLogger");
const { Routes } = require("./configs");
const app = express();
//──── Static Folder
app.use(`/${Routes.Avatars}`, express.static(path.join(__dirname, "public", Routes.Avatars)));
//──── Middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(setHeaders);
app.use(morganLogger);
//──── Routes
app.use(`/${Routes.Users}`, usersRoutes);
app.use(`/${Routes.Games}`, gamesRoutes);
app.use(`/${Routes.Leagues}`, leaguesRoutes);
app.use(`/${Routes.Notices}`, noticesRoutes);
app.use(`/${Routes.Chats}`, chatsRoutes);
//---- WebSocket && https
const options = {
key: fs.readFileSync('./configs/key.pem'),
cert: fs.readFileSync('./configs/cert.pem')
};
const server = createServer(options, app);
bindSocketsToMainServer(server);
//error handler: must be put after all middlewares
app.use(errorHandler);
//──── Connecting To Database
connectToDB()
.then((result) => {
console.log(`Connected To Database`);
server.listen(Routes.Port, () => {
console.log(`Server running on PORT: ${Routes.Port}`);
});
})
.catch((err) => {
console.log(err);
});