-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
108 lines (78 loc) · 2.33 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
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
const path = require('path');
const express = require('express')
const dotenv = require('dotenv')
const logger = require('./Middleware/logger')
const mongoSanitize = require('express-mongo-sanitize');
const helmet = require('helmet');
var xss = require('xss-clean')
const rateLimit = require('express-rate-limit');
const hpp = require('hpp');
const cors = require('cors');
const errorHandler = require('./Middleware/Error')
const cookieParser = require('cookie-parser')
const morgan = require('morgan')
const fileupload = require('express-fileupload')
const colors = require('colors')
// Routes files
const bootcamps = require('./Routes/bootcamps')
const courses = require('./Routes/courses')
const auth = require('./Routes/auth')
const users = require('./Routes/users')
const reviews = require('./Routes/reviews')
// import DB
const connectDB = require("./config/db")
// load env var
dotenv.config({ path: './config/config.env' })
// Conntect MongoDB
connectDB()
const app = express();
// Body parser
app.use(express.json());
// cookie parser
app.use(cookieParser())
// Dev logging Middleware
if (process.env.NODE_ENV === "development")
{
app.use(morgan('dev'))
}
// File uploading
app.use(fileupload())
// data Sanitize
app.use(mongoSanitize());
// set security header
app.use(helmet());
// prevent xss attacks
app.use(xss())
// Rate limiting
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 mins
max: 100
});
app.use(limiter);
// Prevent http param pollution
app.use(hpp());
app.use(cors())
// Set static folder
app.use(express.static(path.join(__dirname, 'public')));
// app.use(logger)
// Mout router
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);
app.use(errorHandler)
// define PORT
const PORT = process.env.PORT || 6000
// app.listen(PORT, () => {
// console.log(`server running in ${process.env.NODE_ENV} mode on port ${PORT}`);
// });
const server = app.listen(PORT, () => {
console.log(`server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold);
});
// handle unhandle promise rejection
process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`.red)
// close server & exit process
server.close(() => process.exit(1))
})