-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
51 lines (40 loc) · 1.44 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
require('dotenv').config(); // Required for environment variables
require('./app/infrastructure/libs/utils');
// add CustomError to globals
global.CustomError = require('./app/domain/exceptions/CustomError');
const helmet = require('helmet');
const express = require('express');
const compression = require('compression');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const { cors } = require('./app/infrastructure/middlewares/cors');
const { errorHandler } = require('./app/infrastructure/libs/errorHandler');
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(compression());
app.use(helmet()); // Add Helmet as a middleware
app.use(cors); // add cors as middleware
// Add Routes and tasks
const routes = require('./app/routes/index');
routes.map((x) => app.use(x.basePath, x.router));
app.use(express.static('public'));
// Main errorHandler
app.use((err, req, res, next) => { errorHandler(err, req, res, next); });
// assume 404 since no middleware responded
app.use((req, res) => {
res.status(404)
.json({
code: 404,
message: 'Not found',
success: false,
data: [],
});
});
const server = app.listen(process.env.PORT || 3000, () => {
const host = server.address().address;
const { port } = server.address();
// eslint-disable-next-line no-console
console.log(`App listening at ${host}:${port}`);
});