-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
41 lines (32 loc) · 1.26 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
require('./config');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('./lib/logger.js');
const app = express();
const port = process.env.PORT || 8080;
app.use(bodyParser.json( { limit: '50MB' } ));
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
next();
});
// Import routes folder
const routePath = './routes/';
const routes = fs.readdirSync(routePath).filter(file => (/.js$/).test(file));
// eslint-disable-next-line global-require, import/no-dynamic-require
routes.forEach(route => require(routePath + route)(app));
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
const errorMessage = err.message || 'Something went wrong!';
logger.error(`Error: ${ statusCode } — ${ errorMessage }`);
res.status(statusCode)
.json({
status: statusCode,
message: errorMessage,
});
});
app.listen(port, () => logger.info(`App listening on port ${ port }`));
module.exports = app;