编辑状态代码时,为什么不能正确表达重定向 [英] Why doesn't express redirect properly when I edit the status code

查看:90
本文介绍了编辑状态代码时,为什么不能正确表达重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准 res.redirect('/ some / path'); 按预期行事并立即将请求重定向到 / some / path ,但如果我添加状态代码,例如 res.redirect(401,'/ some / path'),我会导航到重定向页面, express 不会重定向到 / some / path ,而只是我得到以下页面:

The standard res.redirect('/some/path'); behaves as expected and immediately redirects the request to /some/path, but if I add a status code, e.g., res.redirect(401, '/some/path') and I navigate to the redirecting page, express doesn't redirect to /some/path, instead I just get the following page:

<p>Unauthorized. Redirecting to <a href="/some/path">/</a></p>

并且它永远不会重定向。对于我提供的任何状态代码,这都是相同的。

and it never redirects. This is the same for any status code I supply just by the way.

为什么指定的代码没有重定向工作,因为我期待它,我怎么能返回自定义状态代码并重定向到其他路径?

Why doesn't a code specified redirect work as I'm expecting it to and how can I return a custom status code and redirect to a different path?

推荐答案

位置的行为标头,用于重定向某人,仅针对3xx范围内的状态代码和201/202状态定义。由于您将状态代码设置为401,因此忽略标题并仅呈现响应内容。只是发生Express包含一些很好的文本,解释用户在重定向很慢的情况下被重定向。

The behavior of the Location header, which is used to to redirect someone, is only defined for status codes in the 3xx range, and for 201/202 statuses. Since you are setting the status code to 401, it is ignoring the header and just rendering the response content. It just happens that Express includes some nice text explaining that the user is being redirected in case the redirect is slow.

另外,给定 401 状态代码,您可能会滥用它。 401 代码是让客户知道它需要通过请求发送附加的身份验证信息,例如: http://en.wikipedia.org/wiki/Basic_access_authentication ,因此您不应重定向到另一个网址。

Also, given the definition of the 401 status code, you are likely misusing it. The 401 code is to let the client know that it needs to send additional authentication information with a give request, e.g. http://en.wikipedia.org/wiki/Basic_access_authentication, so you should not be redirecting to another URL.

这篇关于编辑状态代码时,为什么不能正确表达重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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