[英]Ending post request in Node.js
Simple question, but I can't seem to find the answer I need. 简单的问题,但我似乎找不到所需的答案。 I need to end a POST request without sending back the page html as required in res.end(...);
我需要结束POST请求,而不必按res.end(...)的要求发送回html页面。 or res.send(...).
或res.send(...)。
This is my form: 这是我的表格:
<form method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
And this is my request handler in node (I am using exppress): 这是我在节点中的请求处理程序(我正在使用exppress):
app.post( '/' , function( req, res ) {
if(req.body.hasOwnProperty('username') && req.body.hasOwnProperty('password')){
console.log("Login requested",req.body.username);
var minute = 120 * 1000;
var userid = UUID();
res.cookie('user', userid, { maxAge: minute,signed: true});
// I need to end it here
}
});
If I do only res.end(); 如果我只做res.end(); it shows a blank page on the client, I don't want it doing anything.
它在客户端显示空白页,我不希望它做任何事情。 res.send("info");
res.send(“ info”); once again rewrites whole page with the "info".
再次用“信息”重写整个页面。 How do I prevent this?
我该如何预防? Also I need to send back information, whether the login was succesful or not.
我还需要发送回信息,无论登录是否成功。 If I don't return anything, the page is just stuck on loading.. Anybody ever ecountered such problem or can see any solution?
如果我什么也没退,那页面就被卡住了。任何人都遇到过这样的问题,或者可以看到任何解决方案吗?
You could also use res.send()
with a message as you suggested to show that it has ended. 您还可以将
res.send()
与一条消息一起使用,如建议显示该消息已结束。 Or what I do is, assuming this form is in a jade file (let's call it index.jade
), I set up a message as a parameter. 或者我要做的是,假设这种形式在一个玉文件中(我们称它为
index.jade
),我将一条消息设置为参数。
So do something like 所以做类似的事情
res.render("index",
{
isLoggedIn: false
}
);
On the jade layout, you can just check for these variables and display the relevant messages on refresh. 在玉器布局上,您只需检查这些变量并在刷新时显示相关消息即可。 So your form would look like the following
index.jade
file: 因此,您的表单将类似于以下
index.jade
文件:
if isLoggedIn
p Yay!
else
p Login failed
form(method="post")
input(type="text" name="username")
input(type="password" name="password")
input(type="submit" value="Login")
That is fully utilising the power of Express and Jade. 那就是充分利用Express和Jade的功能。 Hope it makes sense!
希望有道理!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.