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.
Using Node JS requires a familiarity with the command line, and if your machine does not already have it, you will need to install it on your machine.
Once you have Node installed, running a Hello World 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.
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.
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.
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.
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.
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.
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.
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 website.