Node Process Manager

Node.js Express Middleware: Error Handling

Nodejs

Free Online Tutorials on JavaScript, Node.js, React.js, PHP, JAVA, Python, HTML, GraphQl, TypeScript, MEAN, MERN Stack, etc.

Express.js middleware in Node.js

Q:- How to handle exception globally using Express.js middleware in Node.js?

In Express.js use - middleware to handle common errors in api/web requests.

// Handle errors
app.use((err, req, res, next) => {
    if (! err) {
        return next();
    }

    res.status(500);
    res.send('500: Internal server error');
});