1 | 'use strict'; |
||
2 | /*eslint no-console: ["error", { allow: ["warn", "error", "log"] }] */ |
||
3 | const express = require('express'); |
||
4 | const app = express(); |
||
5 | const mongoose = require('mongoose'); |
||
6 | const morgan = require('morgan'); |
||
7 | const bodyParser = require('body-parser'); |
||
8 | const routes = require('./app/routes'); |
||
9 | const config = require('config'); |
||
10 | |||
11 | const port = process.env.PORT || config.PORT || 3000; |
||
12 | |||
13 | // DB options |
||
14 | const options = { |
||
15 | server: { |
||
16 | socketOptions: { |
||
17 | keepAlive: 1, |
||
18 | connectTimeoutMS: 3000 |
||
19 | } |
||
20 | }, |
||
21 | replset: { |
||
22 | socketOptions: { |
||
23 | keepAlive: 1, |
||
24 | connectTimeoutMS: 3000 |
||
25 | } |
||
26 | } |
||
27 | }; |
||
28 | |||
29 | // DB connection |
||
30 | const mongoURI = process.env.DBHost || config.DBHost; |
||
31 | mongoose.connect(mongoURI, options); |
||
32 | |||
33 | const db = mongoose.connection; |
||
34 | |||
35 | |||
36 | db.on('error', console.error.bind(console, 'connection error: ')); |
||
37 | |||
38 | if (config.util.getEnv('NODE_ENV') !== 'test') { |
||
39 | // use morgan to log at command line |
||
40 | app.use(morgan('combined')); |
||
41 | } |
||
42 | |||
43 | // Parse application/json and look for raw text |
||
44 | app.use(bodyParser.json()); |
||
45 | app.use(bodyParser.urlencoded({ |
||
46 | extended: true |
||
47 | })); |
||
48 | app.use(bodyParser.text()); |
||
49 | app.use(bodyParser.json({ |
||
50 | type: 'application/json' |
||
51 | })); |
||
52 | |||
53 | routes(app); |
||
54 | |||
55 | app.listen(port); |
||
56 | console.log('Listening on port ' + port); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
57 | |||
58 | module.exports = app; |