【问题标题】:Page redirect is being ignored in Node.JS页面重定向在 Node.JS 中被忽略
【发布时间】:2020-03-05 11:36:57
【问题描述】:

文件夹是这样设置的

-公开

 -landing
      -landing.html
 -Login
      -login.html
 -register
       -register.html

-路线

 -HTMLroutes
 -APIroutes

- server.js

所有被注释掉的都是我尝试过但没有成功的东西

if (bcrypt.compare(req.body.user.password, user.Password)){

            console.log("successful login")
            // res.send({redirect: '/app'}) 
            // res.redirect("app")
            // return res.redirect("/app")
            // res.sendFile(path.join("./public/landing/landing.html"))
            // res.sendFile('./landing/landing.html', { root: "public" })

}

landing.html 是我希望用户看到的页面。我是通过将路由重定向到“/app”之类的东西还是通过加载页面来加载它。

“res.sendFile(path.join...)”是我最了解的,但我的错误是文件路径不是“绝对”

在我的 server.js 中,我使用 express.static("public"),我知道它以某种方式起作用

但是,每次我测试这个时,我都会得到 console.log("successful login") 工作,但没有别的。就像它只是忽略我给它的任何类型的重定向命令。

感谢您的帮助。

【问题讨论】:

    标签: javascript node.js express redirect routing


    【解决方案1】:

    我假设res.sendFile 需要一个绝对路径。您可以使用path.resolve 获取绝对路径,path.join 将只是“加入”或将路径放在一起,路径将“相对”

    【讨论】:

    • 感谢您的输入,我试过了,但它忽略了它,只是给了我“成功登录”的输出
    猜你喜欢
    • 2013-02-19
    • 2018-06-09
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多