-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
67 lines (56 loc) · 1.6 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
65
66
67
'use strict';
/*eslint no-console: ["error", { allow: ["warn", "error", "log"] }] */
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const routes = require('./app/routes');
const config = require('config');
const path = require('path');
const port = process.env.PORT || config.PORT || 3000;
// DB options
const options = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 3000
}
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 3000
}
}
};
// DB connection
const mongoURI = process.env.DBHost || config.DBHost;
mongoose.connect(mongoURI, options);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error: '));
if (config.util.getEnv('NODE_ENV') !== 'test') {
// use morgan to log at command line
app.use(morgan('combined'));
}
// Parse application/json and look for raw text
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.text());
app.use(bodyParser.json({
type: 'application/json'
}));
//API DOC
app.use('/doc', express.static(path.join(__dirname, './apidoc')));
//Enable cross
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");
next();
})
routes(app);
app.listen(port);
console.log('Listening on port ' + port);
module.exports = app;