Difference between location and redirect in node.js

后端 未结 1 858
失恋的感觉
失恋的感觉 2020-12-16 14:25

What is the use of res.location() method? I can use res.redirect() to redirect to a particular URL and i cannot see any change if i use res.location() before res.redirect()<

相关标签:
1条回答
  • 2020-12-16 15:20

    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.

    0 讨论(0)
提交回复
热议问题