服务器发送事件和带有Express.js的HTTP POST 4(Server Sent Events and HTTP POST with Express.js 4)

编程入门 行业动态 更新时间:2024-10-20 20:35:57
服务器发送事件和带有Express.js的HTTP POST 4(Server Sent Events and HTTP POST with Express.js 4)

我正在使用带有express.js 4和node.js的busboy来通过HTTP POST接收一组字段值和浏览器中表单上传的文件。 然后将处理该文件(提取的数据,在数据库中创建的新文档等),其中一些更新可能不成功。 我想要做的是更新用户在处理过程中发生的事情,然后在完成所有操作后更新结果。

要做到这一点,我想使用HTML5服务器发送事件。 在一些人建议之前,我不能使用websockets。

当我宣布:

var eventSource = new EventSource('/API/upload');

在我的浏览器中,我在服务器上看到此URL的HTTP GET 。 我想要的是eventSource从HTTP POST处理程序监听服务器发送的事件,接收和处理上传的元数据和文件的路由。 我的期望是在我的app.post(...处理程序我可以放:

res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }); res.write('\n');

然后使用以下命令下推状态更新:

res.write(message'\n\n'); res.flush(); // I'm using compression

最后用res.status(200).end();终止交换res.status(200).end(); 在完成所有处理,更新等之后。 但是当然浏览器没有监听那个EventSource,而是在GET路由上监听它

那么,我怎么能:

使用EventStream对象配置浏览器,当我实例化它时不会自动发出GET请求? 而是侦听从服务器上的POST事件处理程序发回的事件?

I'm using busboy with express.js 4 and node.js to receive via an HTTP POSTa set of field values and an uploaded file from a form in the browser. The file will then be processed (data extracted, new documents created in the database etc.) with the possibility of some of the updates not being successful. What I want to do is update the user what is going on during the processing and then the outcome when all is done.

To do this I want to use HTML5 server sent events. Before some suggests it, I can't use websockets.

When I declare:

var eventSource = new EventSource('/API/upload');

in my browser I see an HTTP GET on the server to this URL. What I want is for eventSource to listen for server sent events from the HTTP POST handler, the route that receives and processes the uploaded metadata and file. My expectation was that in my app.post(... handler I could put:

res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }); res.write('\n');

and then push down status updates using:

res.write(message'\n\n'); res.flush(); // I'm using compression

finally terminating the exchange with res.status(200).end(); after all the processing, updating etc. is done. But of course the browser isn't listening for that EventSource, it's listening for one on the GET route

So, how can I:

configure the browser with an EventStream object that doesn't automatically make a GET request when I instantiate it? and instead listen for events being sent back from the POST event handler on the server?

最满意答案

大卫。 你不能这样做。 您可以使用PUT请求来定义数据的去向。 例如PUT /API/upload/1234 。 然后,您可以将事件源指向服务器以获取更新。

new EventSource("/API/upload/1234");

这样服务器就可以报告作业的过程。

David. You cannot do it this way. You could use a PUT request to define where the data is going. E.g. PUT /API/upload/1234. Then you could point your event source at the server to get updates.

new EventSource("/API/upload/1234");

That way the server can report on the process of the job.

更多推荐

本文发布于:2023-07-05 06:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   服务器   Express   js   Events

发布评论

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

>www.elefans.com

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