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. 

About Node.js

In recent years, web development has shifted to bring more power from the server to the browser. This boost in the development of JavaScript libraries has expanded JavaScript applications to many different levels.

Node.js has emerged as part of this process - an asynchronous event-driven JavaScript runtime, designed to build scalable network applications. Moreover, Node.js brought JavaScript execution outside of the browser scope, opening the possibility to run and build flexible JavaScript applications in various environments from local development machines all the way to the cloud server.

Node.js is open source, lightweight, and comes with its own package manager, NPM. NPM  gives developers access to hundreds of other open source JavaScript-based libraries that can be plugged into an application.

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.

The node application out-of-the-box has all of the familiar objects and methods of JavaScript language and is available without a need to install any modules. Every file in Node.js application is considered to be a module. All variables declared in the module file are scoped to this module automatically. Every node module has access to the Global objects that are part of Node.js core. Scripts running under Node.js have an object called global as their global object.

  • 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.

Installing Node.js

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

Summary

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! 

Questions Answered

What is Node.js? How do I install Node.js? What is non-blocking event-driven IO?

CONNECT WITH US