-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (61 loc) · 1.68 KB
/
Copy pathserver.js
File metadata and controls
83 lines (61 loc) · 1.68 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
const path = require('path');
const express = require('express');
const dotenv = require('dotenv');
const morgan = require('morgan');
const colors = require('colors');
const cookieParser = require('cookie-parser');
const mongoSanitize = require('express-mongo-sanitize');
const helmet = require('helmet');
const xss = require('xss-clean');
const rateLimit = require('express-rate-limit');
const hpp = require('hpp');
const cors = require('cors');
const errorHandler = require('./middleware/error');
const connectDB = require('./config/db');
// Load env vars
dotenv.config({ path: '.env' });
// Connect to database
connectDB();
// Route files
const auth = require('./routes/auth');
const latency = require('./routes/latency');
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'));
}
// Sanitize data
app.use(mongoSanitize());
// Set security headers
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 to allow access from all domains
app.use(cors());
// Mount routers
app.use('/auth', auth);
app.use('/latency', latency);
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 this ${PORT}`.yellow.bold
)
);
// Handle unhandled promise rejections
process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`.red);
});