-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (33 loc) · 993 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
40
41
42
43
44
45
46
'use strict';
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
module.exports = function(repo){
let api = require('./api')(repo);
/* *********************************
* MIDDLEWARES
* *********************************/
app.use(bodyParser.json());
app.use((req, res, next) => {
console.log(Date(), ' New request');
next();
});
/* *********************************
* API
* *********************************/
app.get('/', (req, res) => res.status(200).send('Hello Beautiful World!'));
app.use('/book', api);
/* *********************************
* ERROR HANDLING
* *********************************/
app.use((req, res, next) => {
let err = new Error('Not found');
err.status = 404;
next(err);
});
app.use((err, req, res, next) => { // jshint ignore:line
console.log('Error: ', err);
res.status(err.status || 500).json({err: err.message});
});
return app;
};