Upload 4.2
This commit is contained in:
9
part4/bloglist/utils/config.js
Normal file
9
part4/bloglist/utils/config.js
Normal file
@@ -0,0 +1,9 @@
|
||||
require('dotenv').config()
|
||||
|
||||
const PORT = process.env.PORT
|
||||
const MONGODB_URI = process.env.MONGODB_URI
|
||||
|
||||
module.exports = {
|
||||
MONGODB_URI,
|
||||
PORT
|
||||
}
|
||||
11
part4/bloglist/utils/logger.js
Normal file
11
part4/bloglist/utils/logger.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const info = (...params) => {
|
||||
console.log(...params)
|
||||
}
|
||||
|
||||
const error = (...params) => {
|
||||
console.error(...params)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
info, error
|
||||
}
|
||||
31
part4/bloglist/utils/middleware.js
Normal file
31
part4/bloglist/utils/middleware.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const logger = require('./logger')
|
||||
|
||||
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: 'unknown 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
|
||||
}
|
||||
Reference in New Issue
Block a user