-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (43 loc) · 1.33 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
const config = require("config");
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const app = express();
const helmet = require("helmet");
app.use(helmet());
app.enable("trust proxy");
const compression = require("compression");
app.use(compression());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const morgan = require("morgan");
app.use(morgan("tiny"));
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGODB_URI);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
const expressSession = require("express-session");
const MongoStore = require("connect-mongo");
const session = expressSession({
secret: process.env.SESSION_SECRET,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
}),
resave: false,
saveUninitialized: true,
cookie: config.cookie,
});
app.use(session);
const port = process.env.PORT || process.env.API_PORT;
const http = require("http").createServer(app);
http.listen(port, () => {
console.log(`Server running on port ${port}`);
});
const router = require("./router");
app.use(config.path, router);
const socket = require("./socket")(http, session);
global.io = socket;
app.get("/", (req, res) => {
res.status(200).json({ ok: true });
});
module.exports = app;