在expressjs中更改res.redirect上的url

San*_*ngh 4 url node.js express angularjs

app.get('/logout', function (req, res){

    req.url='/';
    console.log('req.url is ' + req.url);

    req.session.state = null;

    res.redirect('/login');
});
Run Code Online (Sandbox Code Playgroud)

在重定向时,'url'保持不变.我希望它也能改变它.我尝试使用req.url.但它没有反映在url-bar上.你怎么改变它?

注意: - 我正在使用Angularjs Routing,因此res.redirect不会自动更新url.

编辑: - 角度代码: -

$http({method: 'GET', url: '/logout'}).
        success(function(data, status, headers, config) {
          console.log('happened');

        }).
        error(function(data, status, headers, config) {
          // called asynchronously if an error occurs
          // or server returns response with an error status.
        });
Run Code Online (Sandbox Code Playgroud)

Bre*_*ett 6

默认情况下,res.redirect将发送带有302状态代码的响应和带有您调用该函数的URL的Location标头.所以在你的情况下,例如:

HTTP/1.1 302 
Location: http://example.com/login/
Run Code Online (Sandbox Code Playgroud)

您需要在AngularJS端处理此问题以更改URL.

  • 不确定这个答案是否过时,因为[res.redirect](http://expressjs.com/api.html#res.redirect)允许您现在指定状态代码.只需要`res.redirect(301,'/ login')` (4认同)