-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (75 loc) · 2.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express = require('express');
const app = express();
const http = require('http');
const helmet = require('helmet');
const chalk = require('chalk');
const cors = require('cors');
const listEndpoints = require('express-list-endpoints');
const rateLimit = require('express-rate-limit');
const timeout = require('connect-timeout');
process.env.NODE_ENV = 'local';
const { environment } = require(`./env.${process.env.NODE_ENV}`);
(async () => {
// config environment
await environment();
// stander protocol
app.use(helmet());
app.disable('x-powered-by');
// petitions limit
const limit = process.env.LIMIT_REQUEST.split(',');
let [a, b, c] = limit[0].split('*').map((r) => {
return parseInt(r);
});
const limiter = rateLimit({
windowMs: a * b * c,
max: parseInt(limit[1]),
message: limit[2],
});
app.use(limiter);
// body parser
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
app.use(express.json({ extended: true, limit: '50mb' }));
// cors
app.use(cors({ origin: true, credentials: true }));
app.use((req, res, next) => {
try {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', process.env.HEADERS_ALLOW_HEADERS);
res.header('Access-Control-Allow-Methods', process.env.HEADERS_ALLOW_METHODS);
res.header('Allow', process.env.HEADERS_ALLOW_METHODS);
next();
} catch (error) {
return _res.send({ message: 500 });
}
});
// config db
const database = require('./src/components/database');
await database.connect();
// config routes
const routes = require('./src/configs/routes');
app.use('', routes);
const package = require('./package.json');
app.use('', (req,res)=>{
res.send({
version: package.version,
name: package.name,
author:package.author,
description: package.description,
})
})
/**
* Listen API
*/
http.createServer(app).listen(process.env.PORT, () => {
console.log('listen localhost ' + process.env.PORT);
});
/**
* TimeOut
*/
app.use(timeout(process.env.TIMEOUT, { respond: true }));
// list Endpoints
console.log(chalk.blue('API RULES!: LISTS DE ENDPOINTS'));
listEndpoints(app).forEach((path) => {
console.log(chalk.yellow('http://localhost:' + process.env.PORT + path.path.trim(), chalk.blue(path.methods)));
});
})();