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 trialStanislaus Slupecki
1,986 PointsExpress 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
1,986 PointsYeah, I think it is implied that you are supposed to remove the 500 error handler before moving on to the 404 error
Thomas Dimnet
Python Development Techdegree Graduate 43,629 PointsHi 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
10,321 PointsYup! 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
7,739 PointsEDIT: 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
8,440 PointsHi Brandon, I'm getting the same "RangeError: Invalid status code: 0" message :(
Were you able to fix this?
ac casc
Full Stack JavaScript Techdegree Student 3,778 PointsAndrew Chulkers does not know how to teach. He is very unorganised.
HanJoon Kim
Full Stack JavaScript Techdegree Student 15,388 PointsI totally agree with you
Reid Young
Courses Plus Student 14,209 PointsWould be a nice fix to toss in, guys! I ran into the same issue before debugging. Cheers!
Robin Whitting
5,964 PointsHi 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
Courses Plus Student 13,126 PointsThanks! actually this is what I thought about after following this video... :D now it's more clear!
Matthew McQuain
14,184 PointsMatthew McQuain
14,184 PointsThank you for your answer. This solved my problem. They should really tell us when to remove bits of code like that.