简体   繁体   中英

Is there a way to run a node task in a child process?

I have a node server, which needs to:

  1. Serve the web pages
  2. Keep querying an external REST API and save data to database and send data to clients for certain updates from REST API.

Task 1 is just a normal node tasks. But I don't know how to implement the task 2. This task won't expose any interface to outside. It's more like a background task.

Can anybody suggest? Thanks.

To make a second node.js app that runs at the same time as your first one, you can just create another node.js app and then run it from your first one using child_process.spawn() . It can regularly query the external REST API and update the database as needed.

The part about "Send data to clients for certain updates from REST API" is not so clear what you're trying to do.

If you're using socket.io to send data to connected browsers, then the browsers have to be connected to your web server which I presume is your first node.js process. To have the second node.js process cause data to be sent through the socket.io connections in the first node.js process, you need some interprocess way to communicate. You can use stdout and stdin via child_process.spawn() , you can use some feature in your database or any of several other IPC methods.

Because querying a REST API and updating a database are both asynchronous operations, they don't take much of the CPU of a node.js process. As such, you don't really have to do these in another node.js process. You could just have a setInterval() in your main node.js process, query the API every once in a while, update the database when results are received and then you can directly access the socket.io connections to send data to clients without having to use a separate process and some sort of IPC mechanism.

Task 1: Express is good way to accomplish this task. You can explore: http://expressjs.com/

Task 2:

If you are done with Expressjs. Then you can write your logic with in Express Framework. This task then can be done with node module forever . Its a simple tool that runs your background scripts forever. You can use forever to run scripts continuously (whether it is written in node.js or not)

Have a look:
https://github.com/foreverjs/forever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM