简体   繁体   English

nodejs用数据重定向用户

[英]nodejs redirect user with data

The client will log in by sending a POST request to my server.客户端将通过向我的服务器发送 POST 请求来登录。 My server will check this.我的服务器会检查这个。 If it works, I want to send the welcome page to the client but insert some data into it via a templating engine.如果可行,我想将欢迎页面发送给客户端,但通过模板引擎将一些数据插入其中。 I have everything but the redirect part figured out.除了重定向部分,我什么都有。 This is what I have (I am using handlebars as a templating engine):这就是我所拥有的(我使用把手作为模板引擎):

app.post("/loginAttempt",function(req, res)
{
    var username = req.body.username;
    var password = req.body.password;
        //if credentials are incorrect, data is false
        //otherwise, data is a html file as a string
        var data = await checkCredentials(username,password);
        if(data === false)
        {
            res.send("fail");
        }
        else
        {
            //not a real function, just using this to simplify code for this post
            var temp = compileWithHandlebars("./front-end/welcome.html",{myData: data});
            res.send(temp);
        }
});

The problem with this is it sends a html file as a string instead of redirecting.这样做的问题是它将 html 文件作为字符串而不是重定向发送。 This means the user sees no change in url, so they cannot hit the back button to go back to the login page.这意味着用户在 url 中看不到任何变化,因此他们无法将返回按钮按到 go 返回登录页面。

I am guessing the temp is a string!我猜 temp 是一个字符串!

   app.post("/loginAttempt",function(req, res)
    {
        var username = req.body.username;
        var password = req.body.password;
            //if credentials are incorrect, data is false
            //otherwise, data is a html file as a string
            var data = await checkCredentials(username,password);
            if(data === false)
            {

                res.status(404).send("fail"); //<==
            }
            else
            {
                //not a real function, just using this to simplify code for this post
                //var temp = compileWithHandlebars("./front-end/welcome.html",{myData: data});


                res.redirect("./front-end/welcome.html",{data:myData});
            }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM