-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (31 loc) · 988 Bytes
/
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
const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const userRoute = require('./routes/user');
mongoose.connect("mongodb://localhost/" + process.env.DB_NAME , { useNewUrlParser: true, useCreateIndex: true })
var db = mongoose.connection;
db.on('error', console.error.bind(console,'DB Connection Error'));
db.once('open', function() {
console.log("Database Connected Successfully!");
});
app.use(morgan('dev'));
mongoose.Promise = global.Promise;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/user', userRoute);
app.use((req, res, next) => {
const error = new Error('Not Found');
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
})
});
module.exports = app;