-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
39 lines (30 loc) · 877 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
36
37
38
39
const createError = require('http-errors');
const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('./src/middlewares/logger.middelware');
const routes = require('./src/routes/routes');
const db = require('./src/db/db.bootstrap');
const app = express();
// connect to db
db();
app.use(logger);
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
// define routes
routes(app);
app.use((req, res, next) => {
next(createError(404));
});
app.use((err, req, res) => {
//log as error if it is an internal server error
if (err.statusCode >= 500) {
console.error(err);
} else {
console.error(err);
}
res.status(err.statusCode || 500)
.json({message: 'something went wrong!', error: err})
.send();
});
module.exports = app;