NodeJS 进行重复重定向 [英] NodeJS making repeat redirects

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

问题描述

我正在从 PHP 迁移到 NodeJS,我试图理解为什么中间件重定向会失控.

I am in the process of moving from PHP to NodeJS, I'm trying to understand why a middleware redirect is spinning out of control.

// Check to see if the request includes a session Username
app.use(function (req, res, next) {
  sessionmanager = req.session;
  if(sessionmanager.Username) {
    console.log('Session Details detected');
    next();
  } else {
    console.log('No session details detected, pushing user to login page');
    res.redirect('/login');
  }
});

// User Landing Page
app.get('/', function (req, res) {
  console.log('Request for landing page');
  res.send('Welcome Page!');
});


// User Login Page
app.get('/login', function (req, res) {
  console.log('Request for User login page');
  res.sendFile(path.join(__dirname, './public', 'login.html'));
});

当我访问 localhost:3000 时,它会将我重定向到 localhost:3000/login 但页面无法加载,因为它看起来被重定向了太多次.当我查看控制台日志时,我可以数数;20 多行消息未检测到会话详细信息,将用户推送到登录页面".

When I visit localhost:3000 it redirects me to localhost:3000/login but the page fails to load because it looks like its been redirected too many times. When I look at the console log, I can count; 20+ rows of message 'No session details detected, pushing user to login page'.

据我所知,重定向循环回到 app.use 函数,未能通过 session manager.Username 语句并陷入循环.将流量重定向到/login 页面的最佳方法是什么?我可以在 app.use 中加入 exclude 子句吗?

From what I can guess, the redirect loops back to app.use function, fails to pass the session manager.Username statement and gets stuck in a loop. Whats the best way to redirect traffic to the /login page? Can I put an exclude clause in app.use?

推荐答案

来自阅读文档:https://expressjs.com/en/guide/using-middleware.html您可以设置请求,以便/login 在 app.use 之前出现,这样它就不是重定向循环的一部分.示例:

From reading the docs at: https://expressjs.com/en/guide/using-middleware.html You can set the requests so that /login comes before app.use this way its not part of the redirect loop. Example:

// User Login Page
app.get('/login', function (req, res) {
  console.log('Request for User login page');
  res.sendFile(path.join(__dirname, './public', 'login.html'));
});

// Check to see if the request includes a session Username
app.use(function (req, res, next) {
  sessionmanager = req.session;
  if(sessionmanager.Username) {
    console.log('Session Details detected');
    next();
  } else {
    console.log('No session details detected, pushing user to login page');
    res.redirect('/login');
    //res.sendFile(path.join(__dirname, './public', 'login.html'));
  }
});

// User Landing Page
app.get('/', function (req, res) {
  console.log('Request for landing page');
  res.send('Welcome Page!');
});

现在您可以向/login url 发出请求,如果您没有登录会话,任何经过 app.use 的页面都会将您推回到登录页面.

Now you can make requests to the /login url and any pages past app.use will push you back to the login page if you have not got a session logged in.

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

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