Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript One Solution

next()

Why don't we pass next(err) in the 404 error handler in app.js ?

1 Answer

I think you are referring to this section of the code in app.js

/* ERROR HANDLERS */
/* 404 handler to catch undefined or non-existent route requests */ 
app.use((req, res, next) => {
  console.log('404 error handler called');

  /* TODO 1: Send a response to the client
    - Set the response status to 404
    - Render the 'not-found' view
  */ 
});

Which post-instructions converts to this -

/* ERROR HANDLERS */
/* 404 handler to catch undefined or non-existent route requests */ 
app.use((req, res, next) => {
  console.log('404 error handler called');

  res.status = 404;
  res.render('not-found');

});

For some reason the app.use( ( [the one with 3 params, not 4] ) ... seemed to ALWAYS be run (after the quotes/router part if applicable), regardless whether the user actually triggered an invalid url. I don't know what was wrong, but now it's not doing that anymore (thus I had to edit and update my response hehe).

Anyways, I think we can use next(err) instead of rendering. But you will also get the stack trace message in the error page. For instance, localhost:3000/asdkahdkljah would normally get the not-found.pug page without the stack trace message, but with the next(err) it gets caught in the Global Error handler and it will include the stack trace message.