-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (78 loc) · 2.51 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
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
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const connectDB = require("./config/database");
const errorMiddleware = require("./middlewares/errors");
const ErrorHandler = require("./utils/errorHandler");
const cookieParser = require("cookie-parser");
const fileUpload = require("express-fileupload");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const mongoSanitize = require("express-mongo-sanitize");
const xss = require("xss-clean");
const hpp = require("hpp");
const cors = require("cors");
//setting up config.env file variables
dotenv.config({ path: "./config/config.env" });
//handling uncaught exception
process.on("uncaughtException", (err) => {
console.log(`Error: ${err.message}`);
console.log("Shutting down the server due to uncaught exception");
server.close(() => {
process.exit(1);
});
});
// Connecting to db
connectDB();
// setting up http headers
app.use(helmet());
// setup body parser
app.use(express.json());
// set cookie parser
app.use(cookieParser());
// handle file upload
app.use(fileUpload());
// sanitize data
app.use(mongoSanitize());
// xss clean
app.use(xss());
// rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});
app.use(cors());
// Apply the rate limiting middleware to all requests
app.use(limiter);
// prevent parameter pollution
app.use(hpp());
// Import all routes
const jobs = require("./routes/jobs");
const auth = require("./routes/auth");
const user = require("./routes/user");
const { connect } = require("./routes/jobs");
app.use("/api/v1", jobs);
app.use("/api/v1", auth);
app.use("/api/v1", user);
// handling unhandled routes
app.all("*", (req, res, next) => {
next(new ErrorHandler(`${req.originalUrl} route not found`, 404));
});
// middleware to handle erro
app.use(errorMiddleware);
const PORT = process.env.PORT;
const server = app.listen(PORT, () => {
console.log(`Server started on port ${process.env.PORT} in ${process.env.NODE_ENV} mode`);
});
// ZmxjuMVl3Z1e4Lur
//
// handling unhandling promise rejection
process.on("unhandledRejection", (err) => {
console.log(`Error: ${err.message}`);
console.log("Shutting down the server due to unhandled promise rejection");
server.close(() => {
process.exit(1);
});
});