-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (29 loc) · 887 Bytes
/
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
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'dev';
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
const router = require('./routes');
const config = require('./config');
const morgan = require('morgan');
const cors = require('cors');
const db = config.DB[process.env.NODE_ENV] || process.env.DB;
mongoose.Promise = Promise;
mongoose.connect(db, { useMongoClient: true })
.then(() => console.log('successfully connected to', db))
.catch(err => console.log('connection failed', err));
app.use(cors());
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use('/api', router);
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.json({
error: {
status: err.status,
message: err.message
}
});
next();
});
module.exports = app;