-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
60 lines (48 loc) · 1.31 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
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const config = require('config');
const log = require('./src/helpers/logger.helper');
const dbHelper = require('./src/helpers/db.helper');
const app = express();
const PORT = parseInt(process.env.PORT, 10) || 8080;
// don't show log in test environment
if (config.util.getEnv('NODE_ENV') !== 'test') {
app.use(morgan('combined'));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Database initalize connection
dbHelper.initDatabaseConnection();
/**
* CORS configurations
*/
app.all('/*', (req, res, next) => {
// CORS headers
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method === 'OPTIONS') {
res.status(200)
.end();
} else {
next();
}
});
/**
* Hello world
*/
app.use('/', require('./src/routes/api.routes'));
/**
* Undefined routes return 404 http code
*/
app.use((req, res) => {
res.status(404).send({
message: 'URL not found',
});
});
app.listen(PORT, () => {
log.info(`Running on port: ${PORT}`);
});
module.exports = app;