What a game of Hangman can teach us about Node.js — PART I
Welcome to the wonderful world of Node.js! This is part 1 of a multiple part series exploring node.js concepts.
What Hangman can teach us about Node.js — PART I **Updated**
Welcome to the exciting and versatile world of Node.js! This is Part 1 in a multi-part series where we explore key Node.js concepts through the lens of a simple but instructive project: a command-line Hangman game, written entirely in Node.
Why Hangman? Because it touches on many fundamental Node.js concepts in a fun, interactive, and practical way. If you’re new to Node, this will give you a real-world peek into the power of server-side JavaScript.
✅ You can view and clone the GitHub repo here
🛠️ Getting Started
- Clone the repo from the link above.
- Open a terminal in the root of the project directory and run:
npm installThis installs all required dependencies listed in package.json, ensuring consistency across environments.
3. Once everything is installed, start the application by typing:
node.index.jsThis launched the Hangman game in your terminal.
console.log("* WELCOME TO HANGMAN_NODE!");
console.log(" ========================================== ");
console.log("* You will have 10 chances to guess a random word.");
console.log("* Blank characters show how many letters are in the word.");
console.log("* Correct letter guess? It will appear!");
console.log("* Guess all the letters correctly? You win!");
console.log("* You can quit at any time by typing 'quit'");
console.log("* HAVE FUN!");
console.log(" ========================================== ");This sets the stage for our deeper exploration of Node.js.
🧠 Let’s Talk About Input in Node.js
Handling input is a fundamental concept in Node, especially for CLI applications like our Hangman game. One powerful way to capture user input is using Node’s built-in readline[1] module.
Here’s how we implement it in our index.js:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});Let’s break this down.
📦 Step 1: Require the Module
const readline = require('readline');
/*
The 'readline' core module provides an interface
for reading input from a Readable stream (like process.stdin)
one line at a time. It’s commonly used for building
interactive command-line applications.
*/🔌 Step 2: Creating a Readline Interface
const readline = require('readline');
const rl = readline.createInterface({ // 👈 Turns on readline
input: process.stdin, // 👈 Tells Node to listen for user input via the standard input stream (usually your keyboard)
output: process.stdout // 👈 Tells Node to print any responses or prompts to the terminal (standard output)
});What we’re doing here is instantiating a new readline interface using the readline.createInterface() method. This method doesn’t just “turn on” the module — it creates a wrapper interface around input/output streams, allowing us to interact with the user line-by-line via the terminal.
input: A readable stream (here, process.stdin), to capture user input.
output: A writable stream (here, process.stdout), to display messages.
The returned object, stored in rl, exposes methods like question() and close() which make it easy to build an interactive CLI.
🧠 Understanding process.stdin and process.stdout in Context
Let’s revisit this code snippet:
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});At first glance, it might seem like a simple setup, but there’s a powerful concept at play here — we’re leveraging Node.js’s built-in process module.
📦 What Is the process Module?
Node.js automatically provides access to a special global object called process, which contains information about and control over the current Node.js process. You don’t need to require() it — it’s available globally.
In this case, we’re using two of its most useful stream-based properties:
process.stdin — the standard input stream (typically your keyboard).
process.stdout — the standard output stream (your terminal display).
process.stderr — the standard error stream (used to print error messages).
These are I/O streams that enable real-time communication between your application and the user’s terminal. Node is designed to work with these streams natively, making it ideal for building CLI tools and interactive applications.
⚙️ Other Related Modules (Worth Mentioning)
While not directly used in this Hangman project, it’s useful to be aware of two more powerful Node.js modules:
child_process – Lets you run shell commands or spawn other Node.js scripts from your current process. Great for automation, scripting, and external tool integration.
cluster – Allows you to create multiple instances of a Node.js server to handle high-load scenarios more efficiently by leveraging multiple CPU cores.
We won’t be covering those in this article, as they fall outside the scope of this intro series. However, if you’re interested, I may write a follow-up article to explore them in detail.
👣 Footnotes:
[1] readline module
The readline module was introduced in Node.js v0.1.104 and provides an interface for reading data from a Readable stream (like process.stdin) one line at a time. It’s especially useful for building CLI tools, prompting user input, and debugging small interactive scripts.
Documentation: https://nodejs.org/api/readline.html
In part two we will continue our discussion and go over node’s built-in event system and other fun stuff.