-
Notifications
You must be signed in to change notification settings - Fork 79
/
server.js
160 lines (142 loc) · 3.97 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// external imports
const express = require("express");
const logger = require("morgan");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const engine = require("ejs-mate");
const session = require("express-session");
const cookieParser = require("cookie-parser");
const flash = require("express-flash");
const MongoStore = require("connect-mongo")(session);
const passport = require("passport");
const helmet = require("helmet");
const rateLimit = require("./middleware/rateLimitter.js");
const getRawBody = require("raw-body");
const contentType = require("content-type");
const hpp = require("hpp");
const toobusy = require("toobusy-js");
const cpuPercentage = require("./middleware/cpuUsage");
// custom imports
const cartLength = require("./middleware/middleware");
const secret = require("./config/secret");
const User = require("./models/user");
const Category = require("./models/category");
// get routes
const mainRoutes = require("./routes/main");
const userRoutes = require("./routes/user");
const errorRoutes = require("./routes/error");
const adminRoutes = require("./routes/admin");
const apiRoutes = require("./api/api");
// initialize express
const app = express();
// connect node to mongodb
mongoose.connect(
secret.database,
(err) => {
if (err) {
console.log("Make sure the database server is running " + err);
} else {
console.log("Connected to the database");
}
},
{ useNewUrlParser: true }
);
//Security Middleware for backend infrastructure
app.use(
helmet.frameguard(),
helmet.hsts(),
helmet.noSniff(),
helmet.dnsPrefetchControl(),
helmet.ieNoOpen(),
helmet.referrerPolicy(),
helmet.xssFilter(),
helmet.hidePoweredBy()
);
// Rate limitter for DDOS attacks
app.use(rateLimit());
// Checks whether request size is greater than the permitter value (default added value 2 kb)
// If request size is greater than 2kb rejects the request
app.use(function (req, res, next) {
if (!["POST", "PUT", "DELETE"].includes(req.method)) {
next();
return;
}
getRawBody(
req,
{
length: req.headers["content-length"],
limit: "1mb",
encoding: contentType.parse(req).parameters.charset,
},
function (err, string) {
if (err) return next(err);
req.text = string;
next();
}
);
});
//Server Overload Notifier
app.use(function (req, res, next) {
if (toobusy()) {
// log if you see necessary
console.log(cpuPercentage());
res.send(503, "Server Too Busy");
} else {
next();
}
});
//Prevent HTTP Parameter Pollution
app.use(hpp());
//middleware
app.use(express.static(__dirname + "/public"));
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
session({
resave: true,
saveUninitialized: true,
secret: secret.secretKey,
store: new MongoStore({ url: secret.database, autoReconnect: true }),
})
);
app.use(flash());
// set the view
app.engine("ejs", engine);
app.set("view engine", "ejs");
// make use of our passport module
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
// assign each route the user object
res.locals.user = req.user;
next();
});
app.use((req, res, next) => {
/* search for all categories */
Category.find({}, (err, categories) => {
/*if there is an error return it */
if (err) return next(err);
res.locals.categories = categories;
next();
});
});
app.use(cartLength);
// make use of the routes
app.use(mainRoutes);
app.use(userRoutes);
app.use(adminRoutes);
app.use("/api", apiRoutes);
app.use(errorRoutes);
app.get("/*", (req, res, next) => {
if (typeof req.cookies["connect.sid"] !== "undefined") {
console.log(req.cookies["connect.sid"]);
}
});
// configure the server's listen port and give user feedback
app.listen(secret.port, (err) => {
if (err) throw err;
console.log("Go to http://localhost:" + secret.port + " in your browser");
});