-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
91 lines (70 loc) · 2.04 KB
/
server.js
File metadata and controls
91 lines (70 loc) · 2.04 KB
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
import express from "express";
import dotenv from "dotenv";
import morgan from "morgan";
import fileupload from "express-fileupload"
import cookieParser from "cookie-parser"
import path from "path"
import { DBConnect } from "./config/db.js";
import { errorHandler } from "./middleware/errorHandler.js";
import bootcamps from "./routes/bootcamp.js";
import courses from "./routes/courses.js";
import auth from "./routes/auth.js"
import user from "./routes/user.js"
import reviews from "./routes/reviews.js"
import cors from 'cors'
import mongoSanitize from "express-mongo-sanitize"
import helmet from "helmet"
import xss from "xss-clean"
import rateLimit from 'express-rate-limit'
// Load env vars
dotenv.config({ path: "./config/config.env" });
// Dirname path
const __dirname = path.resolve();
// Database Connection
DBConnect();
const app = express();
// Body parser
app.use(express.json());
// Using cors
app.use(cors())
// Cookie parser
app.use(cookieParser())
// To remove data using these defaults:
app.use(mongoSanitize());
// Set security headers
app.use(helmet())
// Prevent XSS attacks
app.use(xss())
// Rate limiter (50 req per 10 minutes)
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 mins
max: 1000
})
app.use(limiter)
// Check the NODE env
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// File uploading
app.use(fileupload());
// Set static folder
app.use(express.static(path.join(__dirname, 'public')));
// Mount routes
app.use("/api/v1/bootcamps", bootcamps);
app.use("/api/v1/courses", courses);
app.use("/api/v1/auth", auth);
app.use("/api/v1/users", user);
app.use("/api/v1/reviews", reviews)
// Use Error handler
app.use(errorHandler);
const PORT = process.env.PORT || 8080;
const server = app.listen(PORT, () =>
console.log(
`App is running in ${process.env.NODE_ENV} mood on PORT ${PORT}`.yellow.bold
)
);
// Hanlde Unhandled Promise Rejection Warning
process.on("unhandledRejection", (err, promise) => {
console.log(`Error: ${err.message}`.red.bold);
server.close(() => process.exit(1));
});