-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (58 loc) · 1.97 KB
/
index.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
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
// Security Imports here
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const rateLimit = require('express-rate-limit');
const xss = require('xss-clean');
const app = express();
//configuring Path to Config.env
require('dotenv').config()
// Handling uncaught Exception and gracefully shutting down server in Case
process.on('uncaughtException', err => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
// Body Parsing Has been handled in below to lines, fetching incoming req.body
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// 1) GLOBAL MIDDLEWARES
// Set security HTTP headers
app.use(helmet());
// Limit requests from same API
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again in an hour!'
});
app.use('/api', limiter);
// Data sanitization against NoSQL query injection
app.use(mongoSanitize());
// Data sanitization against XSS
app.use(xss());
// Handling Incoming Requests
app.use('/api/dialogflow', require('./server/routes/dialogflow'));
// Serve static assets if in production
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("client/build"));
// index.html for all page routes
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// Dynamically Setting Port
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server Running at ${port}`)
});
// Handling unhandled Rejection and gracefully shutting down server in Case
process.on('unhandledRejection', err => {
console.log('UNHANDLED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});