-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (41 loc) · 1.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
const cors = require('cors');
const express = require('express');
const errors = require('./errors.json');
const port = process.env.PORT || 3005;
const db = require('@evnotify/utils').db;
const errorHandler = require('@evnotify/middlewares').errorHandler;
const logRouter = require('./routes/logs');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const app = express();
// Cross-Origin-Resource-Sharing support
app.use(cors({
credentials: true,
/**
* Handles the origin for CORS request
* @param {String} origin the origin
* @param {Function} callback callback function
*/
origin: (origin, callback) => {
if (!origin || origin === 'null') origin = '*';
callback(null, origin);
}
}));
// route parsing
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
// route handling
app.use('/logs', logRouter);
// unknown route
app.use((_req, _res, next) => next(errors.UNKNOWN_ROUTE));
// error handling
app.use(errorHandler);
db.connect().then(() => {
app.listen(port, () => console.log(`[HTTP] Server started on port ${port}`));
app.emit('server_ready');
}).catch(() => {
console.error('Database connection failed');
process.exit(1);
});
module.exports = app;