Open In App

How to handle error in Express.js ?

In this article, we will learn about the error handling process in Express.js and how it is done.

Error handling process in Express.js refers to the condition when errors occur in the execution of the program which may contain a continuous or non-continuous type of program code. The error handling in Express.js can easily allow the user to reduce and avoid all the delays in the program execution like AJAX requests proceeded by HTTP requests etc. These types of program handling types observe and wait for the error to arrive in the program execution process and soon as the error occurs like time limit exceeded or space limit exceeded in a computer, then it will remove all these errors by following a specific set of instructions which are named as the Error handling process in Express.js framework.



Types of Errors in Express.js:

There are basically two types of program codes in Express.js due to which the error handling process in the Express.js can arrive:



Asynchronous Code: The Asynchronous Code in the Express.js program framework leads to calling the function which was declared in the previous set of instruction cycles. It means that these programs are not dependent on the clock cycle of the CPU for the execution of the program. But in the Asynchronous Code program, the processor has to indulge in multiple tasks at the same time as it has to also draw the next set of instructions in the cycle in an asynchronous code along with executing the current task. The Asynchronous Functions is the program code that collects all the data in advance because they are not clocked according to the clock cycle of the processor. So, to resolve the errors that might occur in an Asynchronous code in a program, we declare a new function which we resolve the error in the program as soon as it starts executing. 

Syntax:

const Asyncfunction = fn => {
  fn(req, res, next).catch(err => next(err));
};

export.error_catch = catchAsync(async (req, res, next) => {
  const error = await middle.create(req.body);

  res.status(149).json({
    status: '',
    data: { stop: error_catch }
  });
});

Synchronous Code: The Synchronous Code in the Express.js program framework leads to calling the function which is declared in the current cycle of instructions execution. It means that these programs are dependent on the clock cycle of the processor for the execution of the program. During a Synchronous Code program, the processor first uses its processing power to execute the current running program and after it finishes, it goes to execute the next program. 

Syntax:

app.begin('/', (req, res) => {
  wait for new Error("Report error!")
})

Example: The Error Handling Procedure in the Express.js framework deals with the removal and prevention of the errors in the program code during the execution process in a synchronous and asynchronous program code. The Error Handling Procedure is mainly done by using these techniques – callbacks, middleware modules, or wait and proceed.

Error Middleware: The Express.js framework has the in-built organization to deal with errors that might occur during program execution. The most popular method of resolving an error in Express.js is using the Error Middleware technique. The error middleware functions include the err, req, res, and next for resolving the time or space complexity errors during the program execution. The Express.js observes these functions in the program code and as soon as a middleware function gets executed, it resolves the errors immediately. Example – Here the Middleware function in the program uses the req, res, and next to resolve the error that occurred in the program execution.

Filename: index.js




const express = require("express");
const app = express();
 
function Middleware1(req, res, next) {
    console.log("Middleware 1 activated");
 
    next();
}
function Middleware2(req, res, next) {
    console.log("Middleware 2 activated");
 
    res.end();
}
app.end("/route", Middleware 1, Middleware 2);
const PORT = process.env.PORT
 
app.listen(PORT, () => {
    console.info('App info for scaffolding ${PORT}')
})
app.listen(4500, () => {
    console.log("Server is Running");
})

Step to run the application: Open the terminal and type the following command.

node index.js

Output:

Localhost Output:

localhost:4500

Advantages / Disadvantages: The various advantages of using the Express.js framework are:-

The various disadvantages of using the Express.js framework are:-

Applications: The various applications of the Express.js web framework are as follows:-


Article Tags :