-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (54 loc) · 1.62 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
58
59
60
61
62
63
64
65
66
67
68
// external imports
const express = require("express");
const http = require("http");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const path = require("path");
const cookieParser = require("cookie-parser");
const moment = require("moment");
// internal imports
const loginRouter = require("./router/loginRouter");
const usersRouter = require("./router/usersRouter");
const inboxRouter = require("./router/inboxRouter");
// internal imports
const {
notFoundHandler,
errorHandler,
} = require("./middlewares/common/errorHandler");
const app = express();
const server = http.createServer(app);
dotenv.config();
// socket creation
const io = require("socket.io")(server);
global.io = io;
// set comment as app locals
app.locals.moment = moment;
// database connection
mongoose.set("strictQuery", true);
mongoose
.connect(process.env.MONGO_CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("database connection successful!"))
.catch((err) => console.log(err));
// request parsers
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// set view engine
app.set("view engine", "ejs");
// set static folder
app.use(express.static(path.join(__dirname, "public")));
// parse cookies
app.use(cookieParser(process.env.COOKIE_SECRET));
// routing setup
app.use("/", loginRouter);
app.use("/users", usersRouter);
app.use("/inbox", inboxRouter);
// 404 not found handler
app.use(notFoundHandler);
// common error handler
app.use(errorHandler);
server.listen(process.env.PORT, () => {
console.log(`app listening to port ${process.env.PORT}`);
});