-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (89 loc) · 2.79 KB
/
index.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
const express = require("express");
const helmet = require("helmet");
const mongoSanitize = require("express-mongo-sanitize");
const xss = require("xss-clean");
const cors = require("cors");
const hpp = require("hpp");
const path = require("path");
const globalErrorHandler = require("./src/middlewares/error.handler");
const HttpLogger = require("./src/utils/http-logger");
const AppLogger = require("./src/utils/app-logger");
const AppError = require("./src/utils/app-error");
const v1Routes = require("./src/routes/v1");
const { connectV1Database } = require("./src/config/database/mongo");
const {
rateLimit,
setupRateLimiter,
teardownRateLimiter,
} = require("./src/middlewares/rate.guard");
require("dotenv").config({
path: `./src/config/envs/.env.${process.env.NODE_ENV}`,
});
process.on("uncaughtException", (err) => {
AppLogger.error(
err ? err.message : "Uncaught exception with no error object"
);
AppLogger.error(err ? err.stack : "No stack trace available");
process.exit(1);
});
const app = express();
const PORT = process.env.PORT || 9000;
app.set("view engine", "pug");
app.set("views", path.join(__dirname, "src/views"));
app.enable("trust proxy");
app.use(HttpLogger);
// parse body json data with limit of 10kb
app.use(express.json({ limit: "10kb" }));
// parse data from urlencoded form
app.use(express.urlencoded({ extended: true, limit: "10kb" }));
// set security HTTP headers
app.use(helmet());
app.use(cors());
// Data sanitization against NoSQL query injection
app.use(mongoSanitize());
app.use(xss());
if (process.env.NODE_ENV !== "development") {
app.use(rateLimit);
}
// protect against parameter pollution
// add the parameters you want to whitelist (to be duplicated in the query string)
app.use(
hpp({
whitelist: [],
})
);
app.use("/api/v1", v1Routes);
app.get("/", (req, res) => {
res.send("RBAC System Server is running");
});
// Middleware for 404 routes
app.all("*", (req, res, next) => {
next(new AppError(`Can’t find ${req.originalUrl} on this server`, 404));
});
// error handler
app.use(globalErrorHandler);
if (require.main === module) {
// This block will only execute if this file was run directly, and not when it was required as a module
let server;
(async () => {
setupRateLimiter();
await connectV1Database();
server = app.listen(PORT, () => {
AppLogger.info(`RBAC System Server listening on port ${PORT} 🚀`);
});
})();
process.on("unhandledRejection", (err) => {
AppLogger.error(
err ? err.message : "Unhandled rejection with no error object"
);
AppLogger.error(err ? err.stack : "No stack trace available");
if (server) {
server.close(() => process.exit(1));
} else {
process.exit(1);
}
// close rate limiter
teardownRateLimiter();
});
}
module.exports = app;