blob: 1b2979a82ad8f43553526baa488608739a5e7ddb (
plain)
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
|
const logger = require('./logger.js')
const requestLogger = (request, response, next) => {
logger.info('Method:', request.method)
logger.info('Path: ', request.path)
logger.info('Body: ', request.body)
logger.info('---')
next()
}
const unknownEndpoint = (request, response) => {
response.status(404).send({ error: 'unkown endpoint' })
}
const errorHandler = (error, request, response, next) => {
logger.error(error.message)
if (error.name === 'CastError') {
return response.status(400).send( {error: 'malformatted id'} )
} else if (error.name === 'ValidationError') {
return response.status(400).json({error: 'error.message'})
}
next(error)
}
module.exports = {
requestLogger,
unknownEndpoint,
errorHandler
}
|