From 14da079ac43e684070258b513b5866ee616c2b12 Mon Sep 17 00:00:00 2001 From: Jones Ogolo <47540149+Jay-Topher@users.noreply.github.com> Date: Sun, 19 Sep 2021 13:34:02 +0000 Subject: [PATCH 1/2] added fourth arg to make the middleware an error handler Prior to this update, the first arg (err) has been seen by express as Request (req) so the handler has never been called --- src/app.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 3f88440..1a52d81 100644 --- a/src/app.ts +++ b/src/app.ts @@ -76,7 +76,13 @@ app.use(function ( }); // error handler -app.use(function (err: any, req: express.Request, res: express.Response) { +// When a middleware has four args, express sees it as an error handler +app.use(function ( + err: any, + req: express.Request, + res: express.Response, + _next: express.NextFunction, +) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; From 898eaddc9abe7d9b63a12daeecb6a905920f66d8 Mon Sep 17 00:00:00 2001 From: Jones Ogolo <47540149+Jay-Topher@users.noreply.github.com> Date: Sun, 19 Sep 2021 13:35:48 +0000 Subject: [PATCH 2/2] Taking out render method Would throw an error because there's no view engine specified (e.g: pug). --- src/app.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 1a52d81..96c6f03 100644 --- a/src/app.ts +++ b/src/app.ts @@ -89,7 +89,9 @@ app.use(function ( // render the error page res.status(err.status || 500); - res.render('error'); + return; + // There's no active view engine so the render method would throw an error in the console + // res.render('error'); }); export default app;