1.

What is spawn in Node JS?

Answer»

The spawn is a method in NodeJS that spawns an external application through a new process and finally RETURNS a streaming interface for I/O. It is EXCELLENT for handling applications that produce a large amount of data or for working with streams of data as it reads in.

Example

Here’s an example how CHILD process have the ability to USE the spawn method:

const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});



Discussion

No Comment Found