-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
79 lines (69 loc) · 2.24 KB
/
index.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
'use strict';
const express = require('express');
const compression = require('compression');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const helmet = require('helmet');
const port = 3000;
const app = express();
const version = 'v0';
require.cache.misc = require('./utils/misc');
// settings
/**
* because you don’t want to make it easy for an attacker to figure what you are
* running The X-Powered-By header can be extremely useful to an attacker for
* building a site’s risk profile
*/
app.disable('x-powered-by');
// settings
/**
* because you don’t want to make it easy for an attacker to figure what you are
* running The X-Powered-By header can be extremely useful to an attacker for
* building a site’s risk profile
*/
app.disable('x-powered-by');
app.use(compression());
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(cookieParser());
app.use(helmet());
// using a single line of code will attach 7 protecting middleware to Express
// appapp.use(helmet());
// additional configurations can be applied on demand, this one mislead the
// caller to think we’re using PHP 🙂
app.use(helmet.hidePoweredBy({
setTo: 'PHP 4.2.0'
})); // other middleware are not activated by default and requires explicit
// configuration .
// app.use(helmet.referrerPolicy({ policy: 'same-origin' }));
// app.use(flash());
app.use('*', (req, res, next) => {
console.log(`URL: ${req.baseUrl}`);
next();
});
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.use((err, req, res, next) => {
if (err) {
res.json(err);
}
});
const endpoints = ['users','assets'];
endpoints.forEach((name) => {
app.use(`/api/${version}/${name}`, require(`./modules/${version}/${name}/routes.js`));
});
app.listen(port)
.on('error',
error => {
console.error(error);
})
.on('listening', () => {
console.log(`Express listening on ${port}`);
// logger.info(`Express listening on ${port}`);
});