0
0
Node.jsframework~20 mins

process.stdin and process.stdout in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use template literals to greet the user and pause the input stream after.