-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (57 loc) · 1.82 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
'use strict';
const express = require('express');
const app = express();
const routes = express.Router();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const mongoose = require('mongoose');
const path = require('path');
require('mongoose-double')(mongoose);
mongoose.Promise = global.Promise;
const config = require('./config/config');
var requestId = require('request-id/express');
app.use(requestId());
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
next();
});
// Swagger API docs.
app.use('/aerodoc/push-configuration-service/docs', express.static(path.join(__dirname, './api-docs')));
app.get('/aerodoc/push-configuration-service/docs', (req, res) => {
res.sendFile(path.join(__dirname, './api-docs/index.html'));
});
// fulfils pre-flight/promise request
app.options('*', function(req, res) {
res.sendStatus(200);
});
(function init() {
var db;
var port;
if (process.env.NODE_ENV === 'test') {
port = 5555;
db = config.test;
} else {
port = process.env.SERVER_PORT || 8080;
db = process.env.MONGO_URL || config.database;
}
app.listen(port, () => {
console.log(`Server started on ${port}`);
});
mongoose.connect(db, { server: { auto_reconnect: true } }, function(err) {
if (err) {
console.log("Error ", err);
} else {
console.log('Connected to database');
}
});
})();
var api = require('./routes');
app.use('/aerodoc/rest', api);
module.exports = app;