Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

How to use express.Router instance for error handling

$
0
0

According to the documentation, any nodejs express middleware function can be replaced by App or Router instances:

Since router and app implement the middleware interface, you can use them as you would any other middleware function.

This is some generic error handling I use:

express()
    .use('/test', new TestRouter())
    .use((err, req, res, next) => {
        console.error(err.stack);
        res.status(500).send(err.message);
    })
    .listen(PORT);

I tried to replace my error handling with an error-handling Router, but now the callback is never executed and express uses it's default error handling.

const handler = new express.Router();
handler.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send(err.message);
});

express()
    .use('/test', new TestRouter())
    .use(handler)
    .listen(PORT);

Why is this not working as expected and how could I solve it?


Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>