-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (30 loc) · 945 Bytes
/
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
require('dotenv').config();
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const port = process.env.PORT || 8000
const app = express();
require('./Configs/globals');
app.use(bodyParser.json()) // Allow application JSON
app.use(bodyParser.urlencoded({ extended: false })) // Allow URL encoded parser
app.use(cors()) // Allowed all cors request origins
// Response Handler
app.use((req, res, next) => {
const ResponseHandler = require('./Configs/responseHandler')
res.handler = new ResponseHandler(req, res);
next()
})
// Routes
const appRoutes = require('./Routes')
appRoutes(app)
// Global Error Handler
app.use ((err, req, res, next) => {
if (res.headersSent) {
return next(err)
}
res.handler.serverError(err)
})
// Start Server
app.listen(port, () => {
console.log(`Server started on ${port} :)`)
})