-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
126 lines (101 loc) · 3.85 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const path = require("path");
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const mongoSanitize = require("express-mongo-sanitize");
const helmet = require("helmet");
const xssClean = require("xss-clean");
const expressRateLimit = require("express-rate-limit");
const hpp = require("hpp");
const cors = require("cors");
const lusca = require("lusca");
const session = require("express-session");
require("colors");
// Internal Imports *****************************************************
const logger = require("./middleware/logger");
const cookieParser = require("cookie-parser");
const fileUpload = require("express-fileupload");
const errorHandler = require("./middleware/error");
const connectDB = require("./db/db");
//Load env vars *******************************************************
// dotenv.config({path: "./config/config.env"});
dotenv.config();
//Connect To DB********************************************************
connectDB().then(() => {
console.log(`Connected to MongoDB`.bgGreen.bold);
});
//Router Files**********************************************************
const bootcamps = require("./routes/bootcampsRoute");
const courses = require("./routes/coursesRoute");
const auth = require("./routes/authRoute");
const users = require("./routes/usersRoute");
const reviews = require("./routes/reviewsRoute");
const app = express();
//Body Parser **********************************************************
app.use(express.json());
//Cookie Parser ********************************************************
app.use(cookieParser());
//Use logger Middleware ************************************************
app.use(logger);
//Use morgan Middleware *************************************************
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
//File Uploading *******************************************************
app.use(fileUpload());
// Sanitize Data *******************************************************
app.use(mongoSanitize());
//Set Security Headers ************************************************
app.use(helmet());
// Prevent XSS attacks ************************************************
app.use(xssClean());
//Rate Limiting ******************************************************
const limiter = expressRateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 100,
});
app.use(limiter);
//Prevent http params pollution **************************************
app.use(hpp());
//Enable CORS ********************************************************
app.use(cors());
// Set up session middleware
app.use(
session({
secret: "your-secret-key",
resave: false,
saveUninitialized: true,
cookie: { secure: true },
})
);
// CSRF Protection *****************************************************
app.use(lusca.csrf());
//Set Static Folder ****************************************************
app.use(express.static(path.join(__dirname, "public")));
// Home Page
app.get("/", (req, res) => {
res.send("<h1>Bootcamp Home Page</h1>");
});
//Mount Routers *********************************************************
app.use("/api/v1/bootcamps", bootcamps);
app.use("/api/v1/courses", courses);
app.use("/api/v1/auth", auth);
app.use("/api/v1/users", users);
app.use("/api/v1/reviews", reviews);
//Add Error Handler *****************************************************
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, () => {
console.log(
`Server Running in ${process.env.NODE_ENV} Mode on Port ${PORT}`.green.bold
.inverse
);
});
//handle unhandled promise rejections ************************************
process.on("unhandledRejection", (error) => {
console.log(`Error: ${error.message}`.bgRed.bold);
// Close server and exit process *****************************************
server.close(() => {
process.exit(1);
});
});