-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (57 loc) · 1.7 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
// third party
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
//
const config = require('./config/config.js');
const cors = require('cors');
const { port } = config.api;
const { initDB } = require('./db/db.js');
// const addEndpoints = require('./jsonapi');
const addEndpoints = require('./endpoints');
const addMiddlewares = require('./middlewares');
const logger = require('./utils/logger.js');
initDB()
.then(() => {
logger.info('Db initializing succeed!');
const app = express();
app.use(bodyParser.json({
limit: '10mb',
type: 'application/vnd.api+json'
}));
app.use(bodyParser.json({
limit: '10mb',
type: 'application/json'
}));
app.use(bodyParser.urlencoded({
limit: '10mb',
type: 'application/x-www-form-urlencoded',
extended: false
}));
let whitelist = [];
whitelist = config.cors.whitelist;
if (config.env === 'dev') {
const morgan = require('morgan');
app.use(morgan('dev'));
}
const corsOptions = {
origin: whitelist
};
app.use(cors(corsOptions));
// app.use(bodyParser.urlencoded({ limit: '10mb', extended: false }));
addMiddlewares(app);
addEndpoints(app);
// handle every other request
app.use('/', (req, res) => {
const warnmsg = `Endpoint not found: ${req.method} ${req.originalUrl}`;
logger.warn(warnmsg);
res.status(404).send(warnmsg);
});
const server = http.createServer(app);
server.listen(port, () => {
logger.info(`API Server is listenning on localhost:${port}`);
});
})
.catch((err) => {
logger.error(`Error during server initializing: ${err.message}`);
});