-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (58 loc) · 1.66 KB
/
app.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
/**
* Import libraries, routers, controllers, database connection
*/
const dotenv = require('dotenv')
dotenv.config()
// Import Libraries
const express = require('express');
var http = require('http');
var timeout = require('connect-timeout')
const logger = require("./libs/logger");
const morgan = require("./middlewares/morgan");
const authentication = require("./middlewares/authentication");
// Import Routers
const apiRouter = require('./routes/api');
// Import Database
const connectDB = require('./libs/mongodb');
// Connect to Database
new Promise(async(resolve, reject) => {
await connectDB(logger);
});
app = express();
httpServer = http.createServer(app);
// Timeout
app.use(timeout('25s'));
/**
*--------- GENERAL SETUP -------------
*/
// Morgan Logger
app.use(morgan);
// add req logger
app.use((req, res, next) => {
req.logger = logger;
req.delay = ms => new Promise(resolve => setTimeout(resolve, ms));
next();
});
// Static folder
app.use(express.static('public'));
app.use(haltOnTimedout);
// Express body-parser
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true }))
app.use(haltOnTimedout);
// Set up cors - GET, POST, PUT, PATCH, and other REQUEST
function haltOnTimedout(req, res, next) {
if (!req.timedout) {
next();
} else {
return res.status(408).json({
success: false,
message: 'Request Timeout'
})
}
}
app.use(haltOnTimedout);
// Set up Routers
app.use('/api', apiRouter); // API Router
app.use(haltOnTimedout);
httpServer.listen(process.env.PORT, () => console.log('App is listening on url http://' + process.env.URL + ':' + process.env.PORT));