-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (25 loc) · 844 Bytes
/
app.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
const app = require('express')();
const bodyParser = require('body-parser');
const apiRouter = require('./routes/api');
const mongoose = require('mongoose');
const cors = require('cors');
const {DB_URL} = process.env.NODE_ENV === 'production'? process.env : require('./config');
const {handle400, handle404} = require('./errors');
mongoose.connect(DB_URL)
.then(() => {
console.log(`Connected to ${DB_URL}`);
})
.catch(console.log);
app.use(cors());
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use('/api', apiRouter);
app.use('/*', (req, res, next) => {
next({status: 404, message: 'Page not found'});
});
app.use(handle404);
app.use(handle400);
app.use((err, req, res, next) => {
res.status(500).send({message: 'Woops! We messed something up on our server'});
});
module.exports = app;