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.
The example I am going to use is based on a ‘Hangman’ game written completely in Node.js. I chose this example because it uses a lot of Node concepts and allows the reader to see a ‘real-life’ example of server-side Javascript in action. (View Github Repo)
- Clone the repo above
2. In a terminal type the following:
npm install
This ensures that the dependencies remain the same on all machines.
3. After dependencies are updated, type the following:
node index.js
This will activate our hangman program and display instructions on how to play the game. Our instructions are simply a bunch of console logs as follows:
console.log("* WELCOME TO HANGMAN_NODE! AKA 'NERDY 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 be made visible.");console.log("* If you guess all the letters you win!");console.log("* You can quit at any time by typing quit");console.log("* HAVE FUN!");console.log(" ========================================== ")
We can now dive into Node.js itself.
The first concept I’ll cover is how to work with inputs in Node.js. There are a number of ways to work with inputs in Node.js. One way is to use the readline module.
Let’s look how we implement Node’s readline module in our index.js file:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
First we ‘require’ the module as follows:
// Require readline
const readline = require('readline'); ...
Next we ‘turn-on’ the readline module by calling the createInterface* method:
* createInterface creates a wrapper interface
// Turning on readline
const rl = readline.createInterface({
So far it is pretty straightforward, but things are ‘bout to get ‘stooopid’! (I can just see my 17 year old daughter covering her face and running out of the room). Please allow me to continue.
Let us have a look at the next two lines of code.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
The first thing to know is that we are making use of another module. This one is called the ‘process’ module. Furthermore, there are (3) IO handles given to all processes:
stdin — represents input
stdout — represents normal output
stderr — representing error output
In a nutshell, the process module allows you to interact with your system processes (other important modules are the child_process module which allows you to execute code on a separate process, and the cluster module which allows the creation of a cluster of HTTP or TCP servers).
we won’t be diving deep into the other modules as it is beyond the scope of this article, however, I may publish another article to cover that topic (thoughts?).
The main thing to know is that the process module provides access to running processes. It’s also worthwhile to understand that Node works with data streams (memory structures that are readable, writable or both). Streams provide a mechanism to easily read or write data from another source.
In part two we will continue our discussion and go over node’s built-in event system and other fun stuff.