Executing external processes with exec()

If you are implementing some service with Node, there may be occasions in which you require to do some heavy processing, and that, as we have mentioned before, is a no-no because you will block all users. If you need to do this kind of work, Node lets you offload the work to an external process, freeing itself and becoming available for continuing work. The external process will work on its own, in an asynchronous fashion, and when it is done, you will be able to process its results. There are several ways of doing this; let's go into them.

The first option to run a separate command is the child_process.exec() method. This will spawn a shell, and execute a given command in it. Whatever output is generated will be buffered, and when the command finishes execution, a callback function will be called with either the produced output, or an error. 

Let's see an example of calling an external process, by accessing the filesystem. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.66.128