-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (55 loc) · 1.78 KB
/
server.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
const path = require('path');
const express = require('express');
const dotenv = require('dotenv');
const colors = require('colors');
const fileupload = require('express-fileupload');
const cookieParser = require('cookie-parser');
const mongoSanitize = require('express-mongo-sanitize');
const helmet = require('helmet');
const xss = require('xss-clean');
const hpp = require('hpp');
const rateLimit = require('express-rate-limit');
const errorHandler = require('./middleware/error');
const connectDB = require('./config/db');
const cors = require('cors');
// Load Environment
dotenv.config();
// Connect to MongoDB
connectDB();
const app = express();
// Middleware
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(fileupload());
app.use(cookieParser());
// Sanitize data - prevent NoSQL ijections
app.use(mongoSanitize());
// Headers for security
app.use(helmet());
// Prevent XSS
app.use(xss());
// Rate limiting
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 mins
max: 100,
});
app.use(limiter);
// Prevent HTTP Parameter Pollution
app.use(hpp());
// Mounting Routers
app.use('/api/v1/bootcamps', require('./routes/bootcamps'));
app.use('/api/v1/courses', require('./routes/courses'));
app.use('/api/v1/auth', require('./routes/auth'));
app.use('/api/v1/reviews', require('./routes/reviews'));
// Error handling middleware
app.use(errorHandler);
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, console.log(`Listening on PORT: ${PORT}`.blue));
// Handle unhandled promise rejections
process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`.red);
// Close server and exit process
server.close(() => process.exit(1));
});