-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (81 loc) · 2.44 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
const express = require("express");
const app = express();
const dotenv = require("dotenv");
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 xssClean = require("xss-clean");
const hpp = require("hpp");
const cors = require("cors");
const bodyParser = require("body-parser");
// Handling uncaught exception
process.on("uncaughtException", (err) => {
console.log(`Error: ${err.message}`);
console.log("Shutting down due to uncaught exception");
process.exit(1);
});
const connectDatabase = require("./config/database");
const errorMiddleware = require("./middlewares/errors");
const ErrorHandler = require("./utils/errorHandler");
// setting up config env files variables
dotenv.config({ path: "./config/config.env" });
// connect to DB
connectDatabase();
// Setup body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// Setup security headers
app.use(helmet());
// Setup body parser
app.use(express.json());
// Set cookie parser
app.use(cookieParser());
// Handle file uploads
app.use(fileUpload());
// Sanitize Data
app.use(mongoSanitize());
// Prevent XSS attacks
app.use(xssClean());
// Prevent Parameter Pollution
app.use(
hpp({
whitelist: ["positions"],
})
);
// Rate Limiting
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 100,
});
// Setup CORS - Accesible by others domains
app.use(cors());
app.use(limiter);
// Import all routes
const jobs = require("./routes/jobs");
const users = require("./routes/auth");
const user = require("./routes/user");
app.use(process.env.API_ROUTE_V1, jobs);
app.use(process.env.API_ROUTE_V1, users);
app.use(process.env.API_ROUTE_V1, user);
// Handle unhandled routes
app.all("*", (req, res, next) => {
next(new ErrorHandler(`${req.originalUrl} route not found`, 404));
});
// Middleware to handle errors
app.use(errorMiddleware);
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(
`Server started on port ${process.env.PORT} in ${process.env.NODE_ENV} mode`
);
});
// Handle unhandled 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);
});
});