如何结束快车。js /节点后响应?

时间:2021-08-23 02:33:17

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

我把一份文件保存到数据库后,我试图停止post请求。

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 NowJS function, which adds the item to the view(for everyone)

    当obj准备好时,它被保存到DB中,然后一个回调触发一个NowJS函数,该函数将该项添加到视图中(对于每个人)

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

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

6 个解决方案

#1


41  

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

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

res.end('It worked!');

Alternatively, you could render a view and 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 required”之类的弹出窗口。这是POST/redirect模式,您可以在http://en.wikipedia.org/wiki/Post/Redirect/Get上阅读更多。

#2


3  

While you can use the underlying end method borrowed from Node's http module, Express.js has its own send method that calls end when appropriate:

虽然可以使用从Node的http模块借来的底层结束方法,但是Express。js有自己的发送方法,在适当的时候调用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;
};

#3


3  

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.

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

#4


1  

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

如果可以修改客户端代码,通过AJAX发出post请求,那么只需使用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请求。

#5


0  

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(

谢谢

);或者一个漂亮的渲染视图?

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

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

#6


0  

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.

最好的方法是在没有路由到新页面的情况下结束快速路由,或者任何混乱的快递要求您不要停留在当前页面上,尽管您有自己的理由是使用窗口。停止,例如表单,使用javascript处理提交,超时应该足够确保数据以POST方法发送。

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

#1


41  

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

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

res.end('It worked!');

Alternatively, you could render a view and 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 required”之类的弹出窗口。这是POST/redirect模式,您可以在http://en.wikipedia.org/wiki/Post/Redirect/Get上阅读更多。

#2


3  

While you can use the underlying end method borrowed from Node's http module, Express.js has its own send method that calls end when appropriate:

虽然可以使用从Node的http模块借来的底层结束方法,但是Express。js有自己的发送方法,在适当的时候调用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;
};

#3


3  

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.

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

#4


1  

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

如果可以修改客户端代码,通过AJAX发出post请求,那么只需使用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请求。

#5


0  

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(

谢谢

);或者一个漂亮的渲染视图?

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

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

#6


0  

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.

最好的方法是在没有路由到新页面的情况下结束快速路由,或者任何混乱的快递要求您不要停留在当前页面上,尽管您有自己的理由是使用窗口。停止,例如表单,使用javascript处理提交,超时应该足够确保数据以POST方法发送。

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