Skip to content

Commit 81ad2b7

Browse files
author
Maximilian Schwarzmüller
committed
added a logger (morgan), nodemon and error handling
1 parent 197f333 commit 81ad2b7

4 files changed

Lines changed: 2303 additions & 2 deletions

File tree

api/routes/orders.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const router = express.Router();
33

4+
// Handle incoming GET requests to /orders
45
router.get('/', (req, res, next) => {
56
res.status(200).json({
67
message: 'Orders were fetched'

app.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
const express = require('express');
22
const app = express();
3+
const morgan = require('morgan');
34

45
const productRoutes = require('./api/routes/products');
56
const orderRoutes = require('./api/routes/orders');
67

8+
app.use(morgan('dev'));
9+
10+
// Routes which should handle requests
711
app.use('/products', productRoutes);
812
app.use('/orders', orderRoutes);
913

14+
app.use((req, res, next) => {
15+
const error = new Error('Not found');
16+
error.status = 404;
17+
next(error);
18+
})
19+
20+
app.use((error, req, res, next) => {
21+
res.status(error.status || 500);
22+
res.json({
23+
error: {
24+
message: error.message
25+
}
26+
});
27+
});
28+
1029
module.exports = app;

0 commit comments

Comments
 (0)