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 Error Handling Middleware

Why do you need the res.status(err.status) line to pass the value to the template?

err already has a err.status set in the middleware function above, so why do we need to set res.status? Is it literally just for the chrome developer tools?

2 Answers

Tobiasz Gala
seal-mask
.a{fill-rule:evenodd;}techdegree
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,529 Points

When you create new Error there is no status property by default. This is why Andrew added a property with a value of 500 in order to display it in a template. When you check network tab you will see that even when we have an error we have different response status because the error was not thrown by the server but by us so we need to specify a status for our response by using res.status()

nico dev
nico dev
20,364 Points

Hi Peter Lu ,

As I understand it, the error object has a status property that we created and set to 500 above, like you said, So if you don't define res.status value as err.status value, the error.pug file will print that original err.status in the <h2> (that is, 500).

However, like you also mentioned, Chrome Dev Tools helps you see more than just the window's display. And if you do that, going to the Network tab, you'll notice that the response status is still 200 (i.e.: the HTTP status code sent with the response is as if everything was alright, when you sent a response with an error, so something's off there, right?). So if what you want is not just to send an error message but also a response with an error, then you need to set up res.status with the error that occurred, which like you said was specified above in err.status.

In other words, the error object handles only the error, and its properties' values can be sent with the response.

The response object, in turn, is the actual response that you send from the server, which can include an error message or other error properties, a redirection, another type of message, etc. They're two different things.

Here it is explained what the res.status actually does.