-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (54 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const app = express();
const corsFile = require('./cors.json')
const helmetFile = require('./helmet.json')
const {
jsonErrorHandler,
notFoundHandler,
trx,
} = require('./core/expressHandle');
const dirRout = './routes/';
global.trx = trx;
global.router = express.Router();
app.use(express.json());
if (process.env.ENABLE_STATMANAGEMENT === 'true') app.use(require('./core/statsManager/mdlw'));
if (process.env.ENABLE_HELMET === 'true') app.use(helmet(helmetFile));
if (process.env.ENABLE_CORS === 'true') app.use(cors(corsFile));
app.use(jsonErrorHandler);
if(process.env.PROCESS_HEADER === 'true'){
app.use(function (req, res, next) {
res.setHeader('X-Process', process.pid);
next();
});
}
try {
const files = walkSync(dirRout);
files.forEach(file => {
if (file.endsWith('.js')) {
try {
const route = require(`./${file}`);
if (process.env.TRACE === 'true' || process.env.NODE_ENV == 'prd') {
log("info", `Ruta ${file} detectada`);
}
app.use(route);
} catch (error) {
log(
'error',
`Existe un inconveniente al importar la ruta ubicada en ${file}: ${error}`,
);
}
}
});
} catch (error) {
log(
'error',
`Existe un inconveniente al recorrer los archivos de rutas: ${error}`,
);
}
app.get('/status', (req, res) => {
res.json({ keep: 'alive' });
});
app.use(notFoundHandler);
module.exports = app;