2013-10-28 33 views
4
app.get('/logout', function (req, res){ 

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

    req.session.state = null; 

    res.redirect('/login'); 
}); 

重定向时,'url'保持不变。我希望它也改变它。我尝试使用req.url。但它不反映在网址栏上。你怎么改变它?更改res.redirect中的url expressjs

注意: - 我正在使用Angularjs路由,因此res.redirect不会自动更新网址。

编辑: - 角代码: -

$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. 
     }); 
+1

您是直接浏览浏览器到'/ logout',还是像'$ http'之类的东西? – robertklep

+0

@robertklep提出编辑: - 它与$ http服务。 –

+1

在这种情况下,您需要在Angular内部执行重定向。 – robertklep

回答

6

默认res.redirect将发送302状态码,并与您通话功能的URL Location头的响应。所以你的情况就像这样:

HTTP/1.1 302 
Location: http://example.com/login/ 

您将需要在AngularJS端处理此更改URL。

+0

我不能改变它表达本身吗?它会工作 –

+1

因为你使用$ http,你将需要在成功回调中处理302响应。我并不真正了解Angular,但我想你可以检查'status'参数来看它是否是302,并将位置标题从'headers'参数中取出并执行像'$ location.url(headers.Location) 。 – Brett

+4

不确定此答案是否过时,因为[res.redirect](http://expressjs.com/api.html#res.redirect)允许您现在指定状态码。只要做'res.redirect(301,'/ login')' – aug