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 Express Basics Middleware Handling 404 Errors

Stanislaus Slupecki
Stanislaus Slupecki
1,986 Points

Express handling 404 errors: 404 middleware not working

So, I'm doing the Express tutorial on 404 errors. It has you use middleware to handle the errors. But it appears that the middleware I made for the 404 error is even being used. Here is the code I used for the 404 error:

app.use((req, res, next) => {
    console.log("404 error");
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

I can't think of what is causing this middleware not to be run. I've tested it multiple times, and it appears that this code just isn't being run. Any ideas?

6 Answers

Stanislaus Slupecki
Stanislaus Slupecki
1,986 Points

Yeah, I think it is implied that you are supposed to remove the 500 error handler before moving on to the 404 error

Matthew McQuain
Matthew McQuain
14,184 Points

Thank you for your answer. This solved my problem. They should really tell us when to remove bits of code like that.

Thomas Dimnet
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Thomas Dimnet
Python Development Techdegree Graduate 43,629 Points

Hi Stanislaus,

I think I am dealing with the same problem and I think the cause is simple:

  • At first, delete or comment your 500 error. It should be at the very top of your page.
  • Then refresh your web browser and try it out.

Below is the snippet of what you need to comment:

app.use((req, res, next) => {
  console.log('Hello');
  // const err = new Error('Oh noes!');
  // err.status = 500;
  next();
});

Then at the very end of your file, just simply do that:

app.use((req, res, next) => {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.use((err, req, res, next) => {
  res.locals.error = err;
  res.status(err.status);
  res.render('error', err);
});

Thomas.

Michael Lauridsen
Michael Lauridsen
10,321 Points

Yup! I think the video before this one cut out prematurely. He ends it by saying "Let's go back and clear out the middleware that we're using to test it." at the very last, but never got to show it.

Brandon Benefield
Brandon Benefield
7,739 Points

EDIT: I did not remove the err from the next(err) at the top of the code

When I comment out the const err = new Error('Oh noes!') and the err.status = 500 I get a RangeError: Invalid status code: 0

I copied your code and still returns invalid status code. Any ideas?

Brian Foley
Brian Foley
8,440 Points

Hi Brandon, I'm getting the same "RangeError: Invalid status code: 0" message :(

Were you able to fix this?

Andrew Chulkers does not know how to teach. He is very unorganised.

Would be a nice fix to toss in, guys! I ran into the same issue before debugging. Cheers!

Hi All

Rather than re-posting the code from Thomas Dimnet who has a working example I thought I would go through a point that I never noticed mentioned in the videos.

In middleware, if you want to use next(); you need to make sure that it is defined in the parameters of the method. e.g.

app.use((req, res, next) => {
next();
}

This stumped me for a while with the RangeError that I was getting.

I hope it will hep others continue their journey.

Robin

Begana Choi
PLUS
Begana Choi
Courses Plus Student 13,126 Points

Thanks! actually this is what I thought about after following this video... :D now it's more clear!