Chaining Express.js 4's res.status(401) to a redirect

后端 未结 3 2028
长发绾君心
长发绾君心 2020-12-14 20:00

I\'d like to send a response code of 401 if the requesting user is not authenticated, but I\'d also like to redirect when the request was an HTML request. I\'ve been finding

3条回答
  •  借酒劲吻你
    2020-12-14 20:25

    You can certainly send a Location: /login header alongside with your 401 page, however, this is ill-advised, and most browsers will not follow it, as per rfc2616.

    One way to do overcome this, is to serve alongside with your 401 page:

    res.set('Content-Type', 'text/html');
    res.status(401).send('`);
    

提交回复
热议问题