0
0
Node.jsframework~10 mins

Why child processes are needed in Node.js - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a child process using Node.js.

Node.js
const { spawn } = require('[1]');
const child = spawn('ls', ['-lh', '/usr']);
Drag options to blanks, or click blank then click option'
Apath
Bfs
Chttp
Dchild_process
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fs' or 'http' instead of 'child_process' to spawn processes.
2fill in blank
medium

Complete the code to listen for data from the child process's stdout.

Node.js
child.stdout.on('[1]', (data) => {
  console.log(`Output: ${data}`);
});
Drag options to blanks, or click blank then click option'
Adata
Bclose
Cexit
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' or 'close' event instead of 'data' to get output.
3fill in blank
hard

Fix the error in the code to correctly handle child process exit.

Node.js
child.on('[1]', (code) => {
  console.log(`Child exited with code ${code}`);
});
Drag options to blanks, or click blank then click option'
Aclose
Bexit
Cdata
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' event instead of 'exit' to detect process end.
4fill in blank
hard

Fill both blanks to create a child process that runs a script and handle errors.

Node.js
const child = require('child_process').[1]('node', ['script.js']);
child.on('[2]', (err) => {
  if (err) console.error('Error:', err);
});
Drag options to blanks, or click blank then click option'
Aspawn
Bexit
Cerror
Dfork
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' event to catch errors instead of 'error'.
Using 'fork' when 'spawn' is intended.
5fill in blank
hard

Fill all three blanks to create a child process, listen for output, and handle exit.

Node.js
const { [1] } = require('child_process');
const child = [2]('ls', ['-la']);
child.on('[3]', (code) => {
  console.log(`Process ended with code ${code}`);
});
Drag options to blanks, or click blank then click option'
Aspawn
Bchild_process
Cexit
Dfork
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'fork' and 'spawn'.
Listening for 'close' instead of 'exit' event.