search

Home  >  Q&A  >  body text

node redirect redirect failed - Stack Overflow

Determine whether the user is logged in and redirect the page. As a result, the browser prompts that the page cannot run normally

typechotypecho2700 days ago941

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-06-24 09:45:14

    Check the code, it’s a loop redirection

    reply
    0
  • typecho

    typecho2017-06-24 09:45:14

    Based on the code you wrote
    Please check the official website document description first:

    app.use([path,] function [, function...])
    //path defaults to “/”, middleware mounted without a path will be executed for every request to the app.
    //意思就是请求路径为/或为空时,所有的请求都会先执行该路由

    Let’s analyze your code again:
    If it is determined that the username does not exist or is empty, it will be redirected, that is, a new request will be sent, and the request will first go through app.use('/', callback), Then it was judged that the username did not exist, and the request was made again.... So there was an infinite redirect
    The final solution:
    You can replace app.use with app.get, or if the conditions are not met When, directly res.render('login'), that is, directly render the login interface instead of jumping

    reply
    0
  • Cancelreply