How to create a Node.js API that can send progress

2019-08-19 06:00发布

问题:

I'm working on a MEAN project as my hobby. I use Node.js as the back end and Angular.js as the front end. Node.js will execute a long lasting task (it takes about 30 minutes to execute this task), I want to display the task status on the view page. How can I achieve this?

回答1:

Maybe socket.io is a good choice.

Socket.IO enables real-time bidirectional event-based communication. And with this, Server can take the initiative to send message to Client.

Visit http://socket.io/ for more info.



回答2:

I think what you are talking about is a streamed request. Take a look at this http://strongloop.com/strongblog/streaming-chunked-html-node-js-data/.

And in the front-end there is a Javascript API method called EventSource that can read a streamed response or as someone else mentioned you can use sockets if you need bi-directional communication.