node.js 如何结束 express.js / node POST 响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9107226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:07:03  来源:igfitidea点击:

How to end an express.js / node POST response?

node.jsexpress

提问by Andrew Plummer

Im trying to just stop the post request after I've saved a document to the DB, ie:

在将文档保存到数据库后,我试图停止发布请求,即:

app.post('/blah'), function(req,res){ 
//my code does a bunch of stuff

res.what do i put here to tell the client browser to just... stop the POST
}

At the moment im simply using res.redirect('back')which works, but the page refresh is totally arbitrary and i would prefer it didnt happen. I had a go at res.end();but that sends the client to a blank page...

目前我只是使用res.redirect('back')哪个有效,但页面刷新是完全任意的,我希望它没有发生。我res.end();尝试了一下,但这会将客户端发送到空白页面...

Thanks in advance.

提前致谢。

edit:

编辑:

I dont think i made myself clear enough in what im doing sorry.

我不认为我在做对不起的事情上说得足够清楚。

Perhaps its bad practice but this is whats happening:

也许这是不好的做法,但这是正在发生的事情:

  1. POST initiates database save function

  2. Browser sits around waiting for a response

  3. When its good and ready, the obj is saved to the DB, then a callback triggers a NowJSfunction, which adds the item to the view(for everyone)

  1. POST 启动数据库保存功能

  2. 浏览器坐在那里等待响应

  3. 当它准备好时,obj 被保存到数据库,然后一个回调触发一个NowJS函数,该函数将项目添加到视图中(适用于所有人)

I did it this way to be non-blocking( so i thought)

我这样做是为了不阻塞(所以我想)

回答by Rohan Singh

You can use res.endand pass in a string that you want to be sent to the client:

您可以使用res.end并传入要发送给客户端的字符串:

res.end('It worked!');

Alternatively, you could render a viewand then end the response:

或者,您可以呈现一个视图,然后结束响应:

res.render('blah.jade');
res.end();

All that said, redirecting the client is actually the best practice. It makes it so that when they hit the back button in their browser, they can move back seamlessly without getting any "POST required" popups or the like. This is the POST/redirect pattern and you can read more about it at http://en.wikipedia.org/wiki/Post/Redirect/Get.

综上所述,重定向客户端实际上是最佳实践。这使得当他们点击浏览器中的后退按钮时,他们可以无缝地向后移动,而不会出现任何“需要 POST”的弹出窗口等。这是 POST/重定向模式,您可以在http://en.wikipedia.org/wiki/Post/Redirect/Get阅读更多相关信息。

回答by jeremytripp

For future reference, you can also do:

为了将来参考,您还可以执行以下操作:

res.redirect('back');

Basically this just redirects the browser back to the screen the request came from, in other words "refreshing" it. It's hacky and I wish there was a better way, but it works.

基本上这只是将浏览器重定向回请求来自的屏幕,换句话说,“刷新”它。这很hacky,我希望有更好的方法,但它有效。

回答by Behrang Saeedzadeh

While you can use the underlying endmethod borrowed from Node's http module, Express.js has its own sendmethod that calls endwhen appropriate:

虽然您可以使用end从 Node 的 http 模块借用的底层方法,但 Express.js 有自己的send方法可以end在适当的时候调用:

/**
 * Send a response.
 *
 * Examples:
 *
 *     res.send(new Buffer('wahoo'));
 *     res.send({ some: 'json' });
 *     res.send('<p>some html</p>');
 *     res.send(404, 'Sorry, cant find that');
 *     res.send(404);
 *
 * @param {Mixed} body or status
 * @param {Mixed} body
 * @return {ServerResponse}
 * @api public
 */
res.send = function(body){
  .
  .
  .

  // respond
  this.end(head ? null : body);
  return this;
};

回答by Nick Mitchinson

If you could modify your client side code to make the post request via AJAX, you could simply use res.end().

如果您可以修改客户端代码以通过 AJAX 发出发布请求,则只需使用 res.end()。

The problem here is that when the browser makes a POST request (not via AJAX), it no longer cares about the current page. It is expecting a new page, so it will load a new page. This is purely with the browser, and I do not believe there is currently a way for you to work around it.

这里的问题是当浏览器发出 POST 请求(不是通过 AJAX)时,它不再关心当前页面。它期待一个新页面,因此它将加载一个新页面。这纯粹是与浏览器有关,我认为您目前没有办法解决它。

Overall, you should just use an AJAX POST request if you can.

总的来说,如果可以,您应该只使用 AJAX POST 请求。

回答by Muhammed karim

Use --> res.status(204).send(); inside the post request

使用 --> res.status(204).send(); 在 post 请求中

if(err) {

   console.log(err);

} else {

   res.status(204).send();

}

回答by syarul

The best way to end the express routing without routing to new page or what ever the mess express want you not to stay on the current page although you have your own reason is to use window.stop, for example a form, you use javascript to handle the submission, the timeout should be enough to ensure the data is send as POST method.

在不路由到新页面的情况下结束快速路由的最佳方法是使用 window.stop,例如表单,您使用 javascript处理提交,超时应该足以确保数据作为 POST 方法发送。

document.myform.mysubmit.click()
    setTimeout(function(){ 
        window.stop() 
    }, 3000);

回答by Colin Ross

The client browser is waiting for a response. So why not give it one with something like res.send('<p>Thank you</p>');or a nice rendered view?

客户端浏览器正在等待响应。那么为什么不给它一个类似res.send('<p>Thank you</p>');或漂亮的渲染视图呢?

The reason res.end()sends the client to a blank page is that you aren't giving the client anything to display.

res.end()将客户端发送到空白页面的原因是您没有给客户端显示任何内容。