Difference between location and redirect in node.js Difference between location and redirect in node.js express express

Difference between location and redirect in node.js


They are very similar in their description, but one does much more. The easiest way to see the difference is look at the source.

res.location just sets the response header. It does not set a response status code or close the response, so you can write a response body if you want, and you have to call res.end() on your own after.

res.redirect on the other hand sets the status to 302, sets the header (using res.location) and sends a nice response body saying that the user is being redirected, and renders a link if their browser doesn't automatically redirect them for some reason.


Kinda off topic but worth a mention if you're going to add res.redirect its a good thing to keep in mind the type of redirects. 301 vs 302 as loganfsmyth said, res.redirect sets the status to 302 by default but this is bad SEO. To change the status code for res.redirect add 301 then the route to redirect to.

Ex-> res.redirect(301, 'new-page');