[英]how to get data from post request in express\nodeJS and redirect user to html file after submit
[英]nodejs redirect user with data
客戶端將通過向我的服務器發送 POST 請求來登錄。 我的服務器會檢查這個。 如果可行,我想將歡迎頁面發送給客戶端,但通過模板引擎將一些數據插入其中。 除了重定向部分,我什么都有。 這就是我所擁有的(我使用把手作為模板引擎):
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);
}
});
這樣做的問題是它將 html 文件作為字符串而不是重定向發送。 這意味着用戶在 url 中看不到任何變化,因此他們無法將返回按鈕按到 go 返回登錄頁面。
我猜 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.