-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (35 loc) · 1.22 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
require('dotenv').config();
const bodyParser = require('body-parser');
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const { logger } = require('./logging/logger');
require('./models/users');
const app = express();
const port = process.env.PORT || 8081;
// mongoose instance connection url connection
mongoose.Promise = require('bluebird');
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
const connectionString = process.env.NODE_ENV === 'production'
? process.env.DB_URL
: process.env.TEST_DB_URL;
mongoose.connect(connectionString, { useNewUrlParser: true });
// Handle the connection event
const db = mongoose.connection;
db.on('error', logger.error.bind(logger, 'connection error:'));
db.once('open', () => {
logger.info('DB connection alive');
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Don't use morgan in test logs
if (process.env.NODE_ENV !== 'test') {
app.use(morgan('dev'));
}
app.use(require('./routes/routes'));
app.listen(port);
logger.info(`API server started on port: ${port}`);
// For testing purposes
module.exports = app;