Learning NodeJS

First Step in Learning NodeJS

Having understood the basics of ES6, Babel & Webpack in the last week, I am hoping to get my feet wet with at least a very basic understanding of NodeJS in the current week. With the official NodeJS website as my starting point I intend to take baby steps with this shiny new technology and with any luck I am hoping to create at least a couple of components that I can use in my current or future projects.

The first thing to note about NodeJS is that it is event-driven and uses a non-blocking I/O model. Both of these terms are super crucial to understand and I will try to explain them with simple examples within this post. But first thing let me try and write a ‘Hello World’ application in NodeJS just to get some confidence going ­čÖé

Since I already have NodeJS installed on my laptop, I don’t have to do much at all. I created a single ‘index.js’ file and wrote the below script.

The above code is pretty straightforward. I am creating a server object using the http module built-in with NodeJs and sending back a “Hello World” message to any clients that connect to it. Also note that I am starting the server on port 8000. But we can choose any arbitrary port that we want. Let me try and start my server.

If I visit “http://localhost:8000” on my browser, it successfully shows the message returned.

Back in my console I also see the console.log messages.

Awesome, at least I know that my server responds and I can try to build and test out some concepts with NodeJS. Let’s start by understanding the core principles of NodeJS.

Event driven, non-blocking architecture is at the heart of NodeJS. I found the following great articles explaining some of this in detail.

Anatomy of an HTTP Transaction

Blocking vs Non-Blocking Overview

Understand NodeJS event architecture

Event Loop (Found this to be a little more complex, but will come back and study in detail later)

Let me try and test out my understanding of how this works. Below is a modified version of my earlier code.

Basically, I have created a variable responseBody with an initial value of 500. myAsyncFunction modifies the value of responseBody using a simple for loop iteration. Finally this value is sent out to the browser by response.write function called on line 21.

If I run the above code here is the output on the browser.

And following are the log messages in my console.

Simply put, although the value of responseBody was changed eventually, the request object did not wait for myAsyncFunction to finish execution. This is for me the biggest learning so for. Compared to languages such as PHP or C#, NodeJS by default works asynchronously. Unless you specify callbacks or chain your events it will simply run through your code without waiting for heavy operations like I/O reads or database processing to finish. In short, NodeJS is all about callbacks.

Let’s modify my earlier code to use a callback method to myAsyncFunction so that we only send a response back to the browser once myAsyncFunction has completed execution.

Quick look at my browser and console tells me it is working as expected.

Super! While I know that the above code examples are trivial, I am at least getting a first hand experience of what to expect with NodeJS. Reading through some NodeJS online discussion forums it seems quite often we forget about this non-blocking behaviour in our code and it leads to silly errors which are quite difficult to debug. I will try to keep this in mind while building more realistic modules.

Leave a Reply