如何结束一个express.js / node POST响应? [英] How to end an express.js / node POST response?

查看:531
本文介绍了如何结束一个express.js / node POST响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

目前,只需使用 res.redirect('back')它的作品,但页面刷新是完全任意的,我宁愿没有发生。我去了一个 res.end(); ,但将客户端发送到空白页...

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...

提前感谢。

编辑:

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启动数据库保存功能

  1. POST initiates database save function

浏览器正在等待响应

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

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)

我以这种方式做到了非阻塞(所以我想)

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

推荐答案

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

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

res.end('It worked!');

或者,您可以渲染视图,然后结束响应:

Alternatively, you could render a view and then end the response:

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

所有这一切,重定向客户端实际上是最佳做法。它使得当他们在浏览器中点击后退按钮时,他们可以无缝移动,而不需要任何POST必需弹出窗口等。这是POST /重定向模式,您可以在 http:// en .wikipedia.org / wiki / Post / Redirect / Get

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.

这篇关于如何结束一个express.js / node POST响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆