代码之家  ›  专栏  ›  技术社区  ›  Pavindu

NEXTJS中的重定向

  •  0
  • Pavindu  · 技术社区  · 7 年前

    我正在开发一个nextjs应用程序,用户应该在其中登录查看内容。如果用户的用户名和密码正确,我想将其重定向到“/”。但是,我的实现似乎不起作用。非常感谢任何建议。

    我一直在搜索关于这个的问题,但是他们都在谈论重定向 getInitialProps 但这对我没有帮助,因为我想从我的自定义Express服务器重定向用户。

    罗杰斯

     async handleSubmit(event) {
      event.preventDefault()
      const { username, password } = this.state
    
      try {
        const response = await fetch('/log_in', {
          method: 'post',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ username, password })
        })
      }
    
     catch (error) {
        console.log(error)
      }
    }  
    

    服务器,JS

    app.post('/log_in', (req,res) => {
        console.log(`login form data`);
        console.log(`Username : ${req.body.username}`);
        console.log(`password : ${req.body.password}`);
        if(req.body.username == "user" && req.body.password == "123"){
            res.redirect('/')
        }
    }) 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Jan Peter    7 年前

    import Router from 'next/router'
    
    export default () => (
      <div>
        Click <span onClick={() => Router.push('/about')}>here</span> to read more
      </div>
    )