0
0
Node.jsframework~10 mins

spawn for streaming processes in Node.js - Interactive Code Practice

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

Complete the code to import the spawn function from the child_process module.

Node.js
const { [1] } = require('child_process');
Drag options to blanks, or click blank then click option'
Aexec
Bfork
Cspawn
DexecFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using exec instead of spawn, which buffers output instead of streaming.
Trying to import a function that does not exist in child_process.
2fill in blank
medium

Complete the code to spawn a child process that runs the 'ls' command.

Node.js
const ls = spawn('[1]');
Drag options to blanks, or click blank then click option'
Adir
Bshow
Clist
Dls
Attempts:
3 left
💡 Hint
Common Mistakes
Using Windows command 'dir' on Unix systems.
Using a non-existent command like 'list' or 'show'.
3fill in blank
hard

Fix the error in the code to correctly handle the data event from the child process stdout stream.

Node.js
ls.stdout.on('[1]', (data) => {
  console.log(`Output: ${data}`);
});
Drag options to blanks, or click blank then click option'
Adata
Berror
Cmessage
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Listening for 'message' which is not an event on stdout streams.
Using 'error' event to get output data.
4fill in blank
hard

Fill both blanks to spawn a process running 'node' with arguments ['-v'] and handle its exit event.

Node.js
const proc = spawn('[1]', ['[2]']);
proc.on('exit', (code) => {
  console.log(`Process exited with code ${code}`);
});
Drag options to blanks, or click blank then click option'
Anode
B-v
C-h
Dnpm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'npm' instead of 'node' for the command.
Using '-h' which shows help, not version.
5fill in blank
hard

Fill all three blanks to create a child process that runs 'grep' to filter 'error' from input, and handle its stdout data event.

Node.js
const grep = spawn('[1]', ['[2]']);
grep.stdout.on('[3]', (data) => {
  console.log(`Filtered output: ${data}`);
});
Drag options to blanks, or click blank then click option'
Agrep
Berror
Cdata
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' event instead of 'data' to read output.
Using wrong command or pattern strings.