Journey Into Node JS!
Tags: javascript, webdev
Published: 2021-08-07 14:08:46

Suppose you want a program that can handle server-side operations, like writing to a database or a file system, but you feel that content management systems like WordPress and Drupal have more features than you need — and may not be worth the maintenance and setup steps.

Or suppose you speak Javascript as your native tongue. but want to try writing a desktop application that can output files.

If you find yourself in this situation or a variant thereof, I recommend exploring Node JS — a lightweight open-source platform for developing server-side applications with Javascript.

As a developer who has mainly used Drupal content management systems or Android apps to interact with databases and file systems, I am finding Node JS to be an exciting foray into using Javascript to run server-side applications using a lightweight solution.

### Requirements

Using Node JS requires a familiarity with the command line, and if your machine does not already have it, you will need to [install](https://nodejs.org/en/download/package-manager/) it on your machine.

### Hello World

Once you have Node installed, running a [Hello World](https://nodejs.org/en/docs/guides/getting-started-guide/) program is pretty straightforward. Putting that out there now because for me, getting something to work is usually my first step on the journey. That said, this post focuses mainly on what Node JS is and why it is unique.

### Background

Node JS, originally released in 2009 by Ryan Dahl, helps to redefine Javascript’s place in the world by making it efficient at running server-side applications.

While it was not the first Javascript server side platform — that credit goes to Netscape’s LiveWire Pro Web in 1996 — it is set apart by being event-driven — in other words, being drivable by events such as user clicks. It is also distinguished by being able to do interact with clients, files, and databases efficiently without holding up other tasks.

And with the right modules, such as [fs (file system)](https://nodejs.org/api/fs.html), [http](https://nodejs.org/api/http.html), and [MySQL](https://www.w3schools.com/nodejs/nodejs_mysql.asp), can enable server programs to interact with the file system, a database, and the browser client.

### What draws me to Node

Of all of Node’s features, the file system capability is currently what draws me to it. While Node is widely used for websites and web applications, I am right now interested in using it to create local desktop applications that can create and modify files, create directories — essentially taking what a user creates and making useful content packages out of it.

### Intro to Blocking and Non-blocking I/O

One “under the hood” feature that makes Node JS useful is non-blocking I/O, or non-blocking input/output.

What does this mean?

I/O, or input/output, refers to a program interacting with the outside world, either by receiving or sending information.

An I/O interaction usually occurs in the context of a **thread**, or a singular sequential flow of control in a program.

While some steps in a thread may involve an I/O operation, others may involve operations within the program. During an I/O operation, the program sends out a request, and gets a response.

### Blocking versus Non-Blocking

If an I/O operation is _blocking_, it means that after a program sends an I/O request, nothing happens in a given thread until a response is received and the operation is complete. If it is _non-blocking_, it means that the thread can keep going with something else while it waits for a response.

In my case, I am primarily interested in I/O operations occuring between the server program and the browser client, as well as the server program and the file system.

Before we look at the details of Non-Blocking I/O, let’s first take a closer look at threads and the processes they reside in.

### Threads and Processes

When a program and its resources are loaded into memory and begins to run, it is called a **process**. Within this process, there are one or more units of execution called **threads**. These can be thought of as mini-processes within a process. In processes with multiple threads, these threads share the same memory.

### Single-Threaded versus Multi-Threaded

Different programming languages have different ways of handling processes and threads. Some, like Javascript, can run just one unit of execution at a time, and are called **single-threaded.** Others, like Java and C#, can multi-task and have multiple threads running within one process.

### Blocking I/O

While Java can follow multiple instructions at a time, it also uses **blocking I/O**, which means that a thread that is reading or writing a file is blocked until it finishes that file task. In other words, it stops what it is doing until the file input/output process is complete before moving on.

And that’s fine if you have Java’s multiple threads, but on a single-threaded Javascript application, it would hold everything up and make your program less efficient than it could be.

### Non-blocking I/O

Because blocking I/O introduces this inefficiency, Node JS has **non-blocking I/O**, which allows it to send out a request to the file system and move on to another operation while it waits for a response to that request.

### Non-blocking operations in Node JS are asynchronous

As an aside, non-blocking operations in Javascript are asynchronous (or async for short), which means that the request-and-response sequence does not require continual attention by the program or occur as consecutive steps. You can start learning about async requests at [w3schools](https://www.w3schools.com/js/js_asynchronous.asp), and a post here on async requests will be coming soon.

### Hope this helps!

This has been a quick look at Node JS and non-blocking I/O. More information about blocking and non-blocking I/O can be found at the [Node JS](https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/) website.