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

编程入门 行业动态 更新时间:2024-10-23 21:37:12
本文介绍了如何结束一个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:

  • POST启动数据库保存功能

  • 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 / 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 en.wikipedia/wiki/Post/Redirect/Get.

  • 更多推荐

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

    本文发布于:2023-10-12 16:49:17,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1485302.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:结束   express   js   node   POST

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!