-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·88 lines (74 loc) · 2.03 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
const express = require('express')
const errorHandler = require('./middleware/error');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean')
const cors = require('cors');
const hpp = require('hpp');
const fileUpload = require('express-fileupload');
const rateLimit = require('express-rate-limit');
// load env variables
let envPath;
if(process.env.NODE_ENV == 'prod')
envPath = './prod.env';
else
envPath = './dev.env';
require('dotenv').config({path: envPath});
const path = require('path');
const helmet = require('helmet');
const apiRouter = require('./api');
// Import DB
const connectDB = require('./config/db');
connectDB();
require('colors');
const app = express();
// Body Parser
app.use(express.json());
// sanitize Data
app.use(mongoSanitize());
// xss-clean
app.use(xss());
// helmet
app.use(helmet({
contentSecurityPolicy: false,
}));
//rate-limit
const limiter = rateLimit({
windowMs: 60 * 1000, // 1 minutes
max: 10000, // limit each IP to 1000 requests per windowMs
});
app.use(limiter);
// hpp
app.use(hpp());
// cors
app.use(cors());
app.options('*', cors());
// file Upload
app.use(fileUpload());
const options = {
dotfiles: 'ignore',
etag: false,
extensions: ['htm', 'html'],
maxAge: '1d',
redirect: false,
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now());
},
};
app.use(express.static(path.join(__dirname, './public'), options));
// Use Routes
app.use('/api/v1/', apiRouter);
const root = require('path').join(__dirname, 'public', 'build')
app.use(express.static(root));
app.get("*", (req, res) => {
res.sendFile('index.html', { root });
})
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
app.listen(PORT, console.log(`Server running on port ${PORT}`.yellow.bold));
// Handle unhandled promise rejections
process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`.red);
// Close server & exit process
// server.close(() => process.exit(1));
});
module.exports = app;