node.js中位置和重定向之间的区别 [英] Difference between location and redirect in node.js

查看:83
本文介绍了node.js中位置和重定向之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

res.location()方法的用途是什么?我可以使用res.redirect()重定向到特定的URL,如果在res.redirect()之前使用res.location(),则看不到任何更改

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()

推荐答案

它们的描述非常相似,但有更多的描述.观察差异的最简单方法是查看源文件.

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只是设置响应头.它不会设置响应状态代码或关闭响应,因此您可以根据需要编写响应正文,之后必须自己调用res.end().

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将状态设置为302,设置标头(使用res.location),并发送一个漂亮的响应正文,指出用户正在重定向,如果浏览器没有自动将其呈现,则呈现链接出于某种原因将它们重定向.

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.

这篇关于node.js中位置和重定向之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆