【发布时间】:2020-01-31 08:03:36
【问题描述】:
客户端将通过向我的服务器发送 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 的变化,因此他们无法点击返回按钮返回登录页面。
【问题讨论】:
-
你用
res.render试过了吗? -
可以分享
compileWithHandlebars方法的代码吗? -
var temp = handlebars.compile(source)(templateData); ---------source是读取为字符串的html文件
-
很难发布,因为它实际上是一个异步调用,所以我的真实代码在回调函数中有 res.send
标签: javascript node.js templates redirect