Express Error Handling – Middleware for Production and Development
Last Updated :
10 May, 2023
Error handling is the process of detecting and responding to errors that occur during the execution of a program or application. In the context of web applications, error handling typically involves detecting errors that occur during the handling of HTTP requests and responding appropriately to those errors. Effective error handling is important because it can help improve the reliability and availability of an application.
Express provides middleware functions that can be used to handle errors that occur during the handling of HTTP requests. These middleware functions are added to the application’s middleware stack and are executed when an error occurs during the processing of a request.
Syntax:
function errorHandler(err, req, res, next) {
// error handling code goes here
}
Error handling in production: Setting up the project.
Step 1: Create a directory for our project and make that our working directory.
$ mkdir gfg
$ cd gfg
Step 2: Use the npm init command to create a package.json file for our project.
npm init -y
Step 3: Now install Express.
npm i express
In this example, we’ve defined an error-handling middleware function that logs the error to the console and sends a generic error message to the client with a 500 Internal Server Error status code. We’ve also defined a route handler for the root URL that simply sends a “Hello World!” message to the client. To test the error handling middleware, we have intentionally caused an error by adding a route handler that throws an error.
Javascript
const express = require( "express" );
const app = express();
app.use( function (err, req, res, next) {
console.error(err);
res.status(500).send( "Internal Server Error" );
});
app.get( "/" , function (req, res) {
res.send( "Hello World!" );
});
app.get( "/error" , function (req, res) {
throw new Error( "Something went wrong!" );
});
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log(`Server listening on port ${port}`);
});
|
Steps to run the code:
Step 1: Run the following code in the terminal:
node index.js
Step 2: Now create a get request to “localhost:3000/error” to get the error. You can use Postman or write the URL in any web browser.
Output:
Error handling in production: In this example, we’ve defined an error handling middleware function that logs the error stack trace to the console and sends the stack trace to the client with a 500 Internal Server Error status code. We’ve also added a check to ensure that this error-handling middleware is only used in development mode. To test the error handling middleware, we have intentionally caused an error by adding a route handler that throws an error.
Javascript
const express = require( "express" );
const app = express();
if (app.get( "env" ) === "development" ) {
app.use( function (err, req, res, next) {
console.error(err.stack);
res.status(500).send(`<pre>${err.stack}</pre>`);
});
}
app.get( "/" , function (req, res) {
res.send( "Hello World!" );
});
app.get( "/error" , function (req, res) {
throw new Error( "Something went wrong!" );
});
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log(`Server listening on port ${port}`);
});
|
Steps to run the code:
Step 1: Run the following code in the terminal:
node index.js
Step 2: Now create a get request to “localhost:3000/error” to get the error. You can use Postman or write the URL in any web browser.
Output:
Difference between Express error handling middleware for production and development:
|
Production |
Development |
Error response |
Generic error message |
Detailed error message with stack trace |
Error logging |
Limited logging, e.g. console |
Extensive logging, e.g. console, file, or service |
Error debugging |
Limited debugging |
Extensive debugging with stack trace |
Error display |
User-friendly error page |
Technical error message or stack trace |
Error information |
Limited information |
Detailed information, e.g. request details |
Error handling time |
Minimise downtime |
Maximise debugging time |
Middleware usage |
Standard middleware |
Conditional middleware based on NODE_ENV |
Share your thoughts in the comments
Please Login to comment...