Getting Started with Node.js
This two-part blog series is about Node.js. In part one of two, Digital Echidna's Drupal Solutions Lead Anna Mykhailova covers the principles of how Node.js works and explores its library structure.
Non-blocking, event-driven IO
Let’s imagine that you came to a very fancy dress boutique where every single customer has their own shopping assistant. Instead of shopping for yourself, you tell your assistant what you need, and in what size. The assistant then goes and finds your requested clothing item, and brings it to you, to try on. If the size doesn’t fit, you wait until the assistant brings you another size to try.
This is how basic Apache server works. It’s synchronous - actions are performed in order and by blocking - meaning the first action needs to be complete before the start of another action. It’s not hard to see that if the store has a lot of customers, it quickly would become really hard to fulfill their needs in a reasonable amount of time.
Now, imagine a store where you have one shopping assistant for all customers. The assistant can bring you several items of clothing and in different sizes. While you are trying them on, it can go help another customer. The assistant can come back to you if you call him. In this case, the assistant is reacting to events happening in the store and acting on them. He is espousing asynchronous behavior -- meaning that he doesn’t wait for one task to end before beginning another. The environment is non-blocking. A request from one customer won’t block a request from another.
This is how Node.js works. It is asynchronous and non-blocking, event-driven IO. Even though Node.js is single threaded due to it being asynchronous, it can “multitask”, which makes it very fast and suitable for cloud hostings.
Structure of Node.js Library
Node.js consists of core and modules part. The core part of the Node contains Global objects - those are the objects that are available on the Global namespace and core modules. Global namespace runs similarly to the browser.
- Global - the global namespace object that contains all global variables
- Console - allows log into console similar to JS in the browser
- Process - allows to interact with the current process - it has a collection of events that allows to hook into the process in various stages and perform needed actions
- URL - URL class
- WebAssembly - The object that acts as the namespace for all W3C WebAssembly related functionality
- Global Timing Objects - setImmediate(), setInterval(), setTimeout()
Node.js comes with the collection of core modules. The core modules are defined within Node.js' source and are located in the lib folder. They are always loaded if its identifier is passed to require() function.
If you are on MacOS or on PC the default method for installing Node.js is to download a pre-built installer for your platform, install it, and make sure to update the $PATH variable as required. It also can be installed through homebrew:
brew install node
In part two of this blog series, I will show you how to work with core modules, and how to install and define your own module!