-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·64 lines (57 loc) · 2.36 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
52
53
54
55
56
57
58
59
60
61
62
63
64
require('dotenv').config();
require('express-async-errors');
const express = require('express');
const helmet = require("helmet");
const cors = require('cors');
const {apiLogger, createMetadata} = require('./config/logger');
const APIError = require('./app/errors/api-error');
const HttpStatus = require('./app/errors/http-status');
const {i18n} = require('./config/i18n.config');
const {port, baseUrl} = require('./config/system');
(async () => {
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
app.use(helmet());
app.use(i18n.init);
app.use('/public', require("./routes/public"));
app.use('/api', require("./routes/api"));
app.get('/', (req, res, next) => {
res.send(res.__('server.root-url.welcome-message', {serviceName: process.env.SERVICE_NAME}));
next();
});
// middlewares
app.use((error, req, res, next) => {
if (error) {
console.error(error);
if (error instanceof APIError) {
if (res.__(error.description))
error.description = res.__(error.description);
if (res.html && res.html === true) {
const html = `<p><i><b>${res.__('error.error')} ${error.httpCode}</b>: ${error.description}</i></p>`;
res.status(error.httpCode).send(html);
} else {
res.status(error.httpCode).json(error);
}
let meta = createMetadata(req, res, error, 'warn');
apiLogger.warn(`${req.method}:${req.url} ${error.httpCode}`, meta);
return next(res);
}
const jsonResponse = {
name: 'Internal server error',
description: res.__('error.internal-error'),
httpCode: HttpStatus.INTERNAL_SERVER
}
res.status(HttpStatus.INTERNAL_SERVER).json(jsonResponse);
let meta = createMetadata(req, res, error, 'error');
apiLogger.error(`${req.method}:${req.url} ${error.httpCode}`, meta);
}
});
app.use((req, res, next) => {
const meta = createMetadata(req, res);
apiLogger.info(`${req.method}:${req.url} ${res.statusCode}`, meta);
});
app.listen(port());
apiLogger.info(`Server started and running on ${baseUrl()}`);
})();