Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express 4 middleware when route is not found (finalhandler not called): how to check for it?

I set up the following Express 4 middleware stack:

const app = express();
const router = express.Router();
router.get('/test', testResponse);
app.use(checkAccessToken);
app.use(router);
app.use(sendResponse);
//Error Handling
app.use(function(err,req,res,next) {
  // ... Do something here
});

function sendResponse(req, res) {
  res.json({
    data: res.locals.data,
    meta: res.locals.meta
  });
}

If I call the server with a route that doesn't exist (like GET /something) the function sendResponse just after the router handler is called and the caller gets a standard response instead of the usual message "Cannot GET /something", coming from the finalhandler module.
I thought instead that the error handler should have been called, but this is not the case.
Is there a way to force the router to emit an error if a route is not found or to check in the standard response handler if a route has not been matched?
I know that I can add a value in res.locals for any route that has a match and check for it in the standard response handler, but I'd like to use the "right" way to do it, rather than using a workaround.

like image 799
Marco Ottolini Avatar asked Dec 04 '22 00:12

Marco Ottolini


1 Answers

You can check req.route.

var express = require('express');
var app = express();
app.use(require('body-parser').urlencoded({extended: false}));

const router = express.Router();

router.use(function(req, res, next) {
    app.locals.test = 0;  
    next();
});

router.get('/', function(req, res, next) {
    app.locals.test = 10;  
    next();
});

router.get('/about', function(req, res, next) {
    app.locals.test = 20;  
    next();
});

router.use(function(req, res, next) {
    if (!req.route)
        return next (new Error('404'));  
    next();
});

router.use(function(err, req, res, next){
    res.send(err.message);
})

router.use(function(req, res){
    res.send(app.locals.test + '');
});

app.use(router);

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
like image 51
Aikon Mogwai Avatar answered Feb 15 '23 00:02

Aikon Mogwai