debug 500 errors in NodeJS,ExpressJS application

singh

In app.js file at end of code write

 // error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  console.error(err);
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: err
  });
}); 

Then create a new ejs file called error.ejs in /views folder and put following code

There was some error
<pre>
<%= message; %>
<br>
<br>
<%= error; %>
</pre> 

This will print error message that the application may encounter during code execution.