9951 explained code solutions for 126 technologies


expressjsHow do I debug my Express.js application?


Debugging an Express.js application is a process that requires the use of a few tools, such as the Node.js debugger, and the Express.js error handler.

First, set a breakpoint in your code where you want to debug. For example, to set a breakpoint at line 10 of your code, you can use the following code:

debugger;

Next, start the Node.js debugger by running the following command:

node debug <script>

Where <script> is the name of your Express.js application.

Once the debugger is running, use the list command to view the code around the breakpoint. Use the step command to step through the code line-by-line. You can also use the watch command to watch the values of variables.

To get more detailed information about errors, you can use the Express.js error handler. To do this, you need to add the following code to your Express.js application:

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

This will log the stack trace of any errors that occur during the execution of your Express.js application.

In summary, debugging an Express.js application requires the use of the Node.js debugger and the Express.js error handler.

Helpful links

Edit this code on GitHub