-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (37 loc) · 1.12 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require('express')
const app = express();
const mongoose = require('mongoose')
require('dotenv/config')
const bodyParser = require('body-parser')
//Connect to DB
mongoose.connect(
process.env.DB_CONNECTION,
{ useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false },
() => {
console.log('Connected to Db')
})
mongoose.connection.on('error', (err) => {
console.error(`Mongoose connection error: ${err}`);
process.exit(1);
});
//Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// catch 400
app.use((err, req, res, next) => {
console.log(err.stack);
res.status(400).send(`Error: ${res.originUrl} not found`);
next();
});
// catch 500
app.use((err, req, res, next) => {
console.log(err.stack)
res.status(500).send(`Error: ${err}`);
next();
});
//Import routes
const countriesRoute = require('./routes/countriesroute')
// Register the routes
app.use('/', countriesRoute);
const port = process.env.PORT || '3000';
app.listen(port, () => console.log(`Listening on port ${port}`))