Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Basic Input and Output with process.stdin and process.stdout in Node.js
📖 Scenario: You are creating a simple Node.js program that reads a user's name from the keyboard and then writes a greeting message back to the screen.
🎯 Goal: Build a Node.js script that uses process.stdin to read user input and process.stdout to display a greeting message.
📋 What You'll Learn
Use process.stdin to read input from the user
Use process.stdout.write to display messages
Handle the input data event to capture user input
End the input stream after reading the user's name
💡 Why This Matters
🌍 Real World
Reading user input and writing output is essential for command-line tools and scripts that interact with users.
💼 Career
Understanding process.stdin and process.stdout is important for backend developers working with Node.js to build CLI applications or handle streams.
Progress0 / 4 steps
1
Set up input stream and prompt message
Write code to set process.stdin to resume mode and use process.stdout.write to display the message 'Enter your name: '.
Node.js
Hint
Use process.stdin.resume() to start reading input and process.stdout.write() to show the prompt.
2
Set encoding for input stream
Add a line to set the encoding of process.stdin to 'utf8' so the input is read as text.
Node.js
Hint
Use process.stdin.setEncoding('utf8') to read input as a string.
3
Read user input and store it
Use process.stdin.on('data', callback) to listen for input data. Inside the callback, save the input to a variable called name after trimming whitespace.
Node.js
Hint
Listen for the 'data' event on process.stdin and trim the input string.
4
Write greeting and end input stream
Inside the data event callback, use process.stdout.write to display `Hello, ${name}!\n`. Then call process.stdin.pause() to stop reading input.
Node.js
Hint
Use template literals to greet the user and pause the input stream after.
Practice
(1/5)
1. What does process.stdin do in a Node.js program?
easy
A. It manages file system operations.
B. It reads input typed by the user in the terminal.
C. It writes output to the terminal screen.
D. It handles network requests.
Solution
Step 1: Understand the role of process.stdin
process.stdin is used to read data from the terminal where the user types input.
Step 2: Differentiate from process.stdout
process.stdout is for output, not input. File system and network are unrelated here.
Final Answer:
It reads input typed by the user in the terminal. -> Option B
B. Using console.log instead of process.stdout.write.
C. Incorrect event name; should be 'input' not 'data'.
D. Missing parentheses after toString method call.
Solution
Step 1: Check method usage on input
input.toString is a method and needs parentheses to call it: input.toString().
Step 2: Verify event and method correctness
data event is correct for reading input. Using console.log is valid for output.
Final Answer:
Missing parentheses after toString method call. -> Option D
Quick Check:
Method calls need () = D [OK]
Hint: Call methods with () to avoid undefined output [OK]
Common Mistakes:
Forgetting parentheses on toString
Thinking event name is wrong
Believing console.log can't print input
5. You want to create a Node.js program that asks the user for their name, then prints 'Hello, [name]!' and exits. Which code snippet correctly uses process.stdin and process.stdout to achieve this?
hard
A. process.stdout.write('Enter your name: ');
process.stdin.on('data', data => {
process.stdout.write(`Hello, ${data.toString().trim()}!\n`);
process.exit();
});
B. process.stdin.write('Enter your name: ');
process.stdout.on('data', data => {
process.stdout.write(`Hello, ${data}!\n`);
});
C. console.log('Enter your name: ');
process.stdin.on('input', data => {
console.log('Hello, ' + data);
});
D. process.stdout.write('Enter your name: ');
process.stdin.on('data', data => {
console.log('Hello, ' + data);
});
Solution
Step 1: Prompt user correctly
process.stdout.write is used to show the prompt without newline.
Step 2: Read input and trim newline
data.toString().trim() converts input buffer to string and removes newline characters.
Step 3: Output greeting and exit
Print greeting with template string and call process.exit() to end program.
Final Answer:
Code snippet A correctly prompts, reads, trims, outputs, and exits. -> Option A
Quick Check:
Prompt + trim input + exit = A [OK]
Hint: Trim input and call process.exit() after output [OK]
Common Mistakes:
Using stdin.write instead of stdout.write for prompt