Prasheel
Prasheel

Reputation: 1020

What is the difference between res.setHeader('status',400); and res.status(400);?

I'm not sure what the exact difference is. Also, which one is better to be used to set response status?

Upvotes: 0

Views: 728

Answers (1)

Orelsanpls
Orelsanpls

Reputation: 23515

Express documentation says about res.status(code)

Sets the HTTP status for the response. It is a chainable alias of Node’s response.statusCode.


Node.js documentation says about response.statusCode

When using implicit headers (not calling response.writeHead() explicitly), this property controls the status code that will be sent to the client when the headers get flushed.


And tho about setHeaders

For this option, specify a function to set custom response headers. Alterations to the headers must occur synchronously.


Summary

res.setHeader manipulate synchronously the headers (right now).

res.status will set the headers when they 'll get flushed.

Upvotes: 1

Related Questions