-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (65 loc) · 1.77 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
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* 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("./lib/logger");
const morgan = require("./middlewares/morgan");
// Import Routers
const apiRouter = require('./routes/api');
// Import Database
const connectDB = require('./config/database');
// Connect to Database
connectDB(logger);
app = express();
httpServer = http.createServer(app);
// Reveal ip when req.ip
app.set('trust proxy', true)
// 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);
// view engine ejs
app.set('view engine', 'ejs')
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);
async function main() {
await Promise.all([
httpServer.listen(process.env.HTTP_PORT, () => console.log('App is listening on url http://' + process.env.API_URL + ':' + process.env.HTTP_PORT)),
])
}
main();