@ancxkush/create-ts-express-mongo-starter-code
Version:
NodeJS/Express starter code with - TypeScript, MongoDB setup, Exception Handler, Logger, HTTP testing example, Swagger Docs example, Mongoose model example, JOI validation example, CRUD operations example
24 lines (22 loc) • 563 B
text/typescript
import { NextFunction, Request, Response } from 'express'
import logger from './winstonLogger'
//executed when any middeware executes next(error)
//make sure this is the endpoint i.e. last middleware in app
export const errorHandler = (
err: any,
_req: Request,
res: Response,
next: NextFunction
) => {
logger.error(err.message)
const status: number = err.status || 500
const message: string =
status === 500 ? 'Internal server error.' : err.message
res.status(status).send({
error: {
status,
message,
},
})
next()
}