节点表达太多重定向 [英] node-express too many redirects

查看:73
本文介绍了节点表达太多重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路线设置如下

当我导航到' http://localhost/'时,我收到一条错误消息,提示"localhost重定向了您太多次"并且页面的URL(显示在浏览器的URL栏中)为 http://localhost/!/dashboard -因此它的确看起来像是被重定向了,但我看不到为什么它陷入无限循环

When i navigate to 'http://localhost/' i get an error saying 'localhost redirected you too many times' and the URL of the page (showing in the URL bar of the browser) is http://localhost/!/dashboard - so it does look like it is being redirected, but i cannot see why it's getting stuck in an infinite loop

 // Public Routes
 app.use('/', function(req,res){
      res.redirect('/!/dashboard');
 });
 app.use('/login', routes.login);
 app.use('/!/dashboard', isLoggedIn, routes.dashboard);

// Check If Logged In
function isLoggedIn(req,res,next){
    if (req.isAuthenticated()){
      return next();
    } else {
      res.redirect('/login');
    }
};

推荐答案

您不应使用app.use('/', ...),因为它将与任何以/开头的URL 开头相匹配.

You shouldn't use app.use('/', ...), because that will match any URL starting with a /.

相反,请使用 app.all :

Instead, use app.all:

app.all('/', function(req,res){
  res.redirect('/!/dashboard');
});
app.use('/login', routes.login);
app.use('/!/dashboard', isLoggedIn, routes.dashboard);

这篇关于节点表达太多重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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