-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
92 lines (85 loc) · 2.66 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const path = require("path");
const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const http = require("http");
const io = require("socket.io");
const bodyParser = require("body-parser");
const logger = require("morgan");
const session = require("express-session");
const mongoose = require("mongoose");
const createError = require("http-errors");
const expressSanitizer = require("express-sanitizer");
class Server {
constructor() {
this.dbUserName = 'sechat';
this.dbPassword = 'admin1234';
this.dbURL = `mongodb://${this.dbUserName}:${
this.dbPassword
}@ds039768.mlab.com:39768/sechat`;
mongoose.connect(
this.dbURL,
{
useNewUrlParser: true
}
);
this.db = mongoose.connection;
this.db.once("open", () => {
console.log("Connected to mongodb server");
});
this.port = process.env.PORT || 4000;
this.host = "0.0.0.0";
this.app = express();
this.http = http.createServer(this.app);
this.io = io(this.http);
}
setConfig() {
this.app.set("views", path.join(__dirname, "app/views"));
this.app.set("view engine", "ejs");
this.app.set("layout", "layout");
this.app.use(expressLayouts);
this.app.use(bodyParser.json());
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use(expressSanitizer());
this.app.use(logger("dev"));
this.app.use(express.static(path.join(__dirname, "public")));
this.app.use(
session({
rolling: true,
saveUninitialized: false,
resave: false,
secret: "secret-key",
store: require("mongoose-session")(mongoose)
})
);
}
includeRoutes() {
require("./app/sockets")(this.io);
this.app.use((req, res, next) => {
res.locals.title = "CSSM";
res.locals.user = req.session.user;
next();
});
this.app.use("/", require("./app/routers/login"));
this.app.use("/", require("./app/routers/calendar"));
this.app.use("/", require("./app/routers/group"));
this.app.use("/", require("./app/routers/home"));
this.app.use("/", require("./app/routers/invite"));
this.app.use((req, res, next) => next(createError(404)));
this.app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
res.status(err.status || 500);
res.render("error");
});
}
run() {
this.setConfig();
this.includeRoutes();
this.http.listen(this.port, this.host, () => {
console.log(`Listening on http://${this.host}:${this.port}`);
});
}
}
const app = new Server();
app.run();