-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
83 lines (64 loc) · 1.91 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
const express = require("express");
const path = require('path');
const dotenv = require("dotenv");
const morgan = require("morgan");
const { errorHandler, unknownEndpoint } = require('./middleware/error');
const connectDB = require("./config/db");
// const cookieParser = require('cookie-parser');
const mongoSanitize = require('express-mongo-sanitize');
const helmet = require("helmet");
const rateLimit = require('express-rate-limit');
const hpp = require('hpp');
const cors = require('cors');
const xss = require('xss-clean');
// listen env var
dotenv.config({ path: "./config/config.env"});
//connect db
connectDB();
const app= express();
// body parser
app.use(express.json())
//cookie parser
// app.use(cookieParser());
// To remove data, use:
app.use(mongoSanitize());
// use helmet
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());
// Enable CORS
app.use(cors());
// route files
const bootcamps = require('./routes/bootcamps');
const auth = require('./routes/auth');
const room = require('./routes/room');
const code = require('./routes/code');
//dev logging middleware
if(process.env.NODE_ENV === "development"){
app.use(morgan('dev'))
}
//mount routes
app.use('/api/v1/bootcamps',bootcamps);
app.use('/api/v1/auth', auth);
app.use('/api/v1/rooms', room);
app.use('/api/v1/code', code);
// use error handler
app.use(errorHandler);
app.use(unknownEndpoint)
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT,()=>{
console.log(`server runing in ${process.env.NODE_ENV} mode on port ${PORT}`)
});
process.on("unhandledRejection",(error, promise) =>{
console.log(`error: ${error.message}`)
// close the server and exit process
server.close(()=>process.exit())
})