【问题标题】:Express 401 redirect is not happening automaticallyExpress 401 重定向不会自动发生
【发布时间】:2016-11-20 10:32:36
【问题描述】:

遇到一种情况,当用户无权访问页面时,我使用 401 重定向用户。我的一条路线中有这个:

 res.redirect(401, '/login');

看起来很简单,但应用程序会徘徊并且不会将用户重定向到/login。它只是位于与此类似的页面上:

 Unauthorized. Redirecting to /login

标题看起来也不错:

HTTP/1.1 401 Unauthorized
X-Powered-By: Express
Location: /login
Vary: Accept
Content-Type: text/html; charset=utf-8
Content-Length: 63
set-cookie: connect.sid=stuff; Path=/; HttpOnly
Date: Sun, 20 Nov 2016 02:20:07 GMT
Connection: keep-alive

奇怪的是,如果我省略状态,重定向会正常发生:

 res.redirect('/login'); // doesn't hang on page, redirect occurs immediately

我的印象是重定向是自动的,但也许不是?有什么见解吗?

【问题讨论】:

    标签: node.js redirect express


    【解决方案1】:

    401 不是重定向状态。只需使用常规 (303) 重定向 - 无论如何401 is invalid if you’re not sending a WWW-Authenticate header

    【讨论】:

    • 有道理! +1 快速响应
    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    相关资源
    最近更新 更多