Codepath

Introduction to Node.js

Overview

"Node.js" is several things:

  • A set of IO-bindings for the V8 JavaScript VM
  • A package and module system (require(), node_modules)
  • An ecosystem of packages and modules (npm)
  • A software writing philosophy

"Core is small. Userland is large."

Keep this in mind while writing code in the node.js ecosystem. The small number of core modules and the multiplicity of 3rd-party packages is node.js' solution to the standard library problem: "The standard library is where modules go to die."

In languages with a standard library, frequently the core modules become bloated, stagnated and are crippled by being coupled to the language version and the sheer amount of code that depends on them. The node.js project takes a different approach by focusing on only providing IO-bindings and a very small handful of utility libraries to increase operability (e.g., Stream, EventEmitter, util).

What Makes Node.js Different?

There are 3 defining characteristics that make node.js unique:

  1. Asynchronous (non-blocking) IO (single-threaded event-loop runtime)
  2. JavaScript: Simplicity, pervasiveness, reusable on front-end
  3. Ecosystem: The web & JavaScript are not owned by anyone resulting in an explosion of innovation

Typically though, it's the asynchronous (non-blocking) IO that is the focus when discussing node.js. This makes node.js an excellent choice for IO-bound tasks, but a poor choice for CPU-bound tasks.

Asynchronous (Non-blocking) IO

When we say non-blocking IO, we mean that execution of the program does not wait for an IO operation (e.g., filesystem read/write, database call, network request, ...) to complete before proceeding:

// Bad (blocking)
let data = fs.readFileSync(filePath)
console.log(data)                       // Execute BEFORE somethingUnrelated()
somethingUnrelated()

// Good (non-blocking)
fs.promise.readFile(filePath)
    .then(console.log)                  // Execute AFTER somethingUnrelated()
somethingUnrelated()

The primary benefit and virtue of non-blocking IO and an event-loop based runtime is cooperative multi-tasking (allowing for reduced latency for IO-bound tasks) and a much lower memory footprint due to the elimination of thread overhead.

Hello World

First, setup your environment.

Second, you have 3 options for writing your node.js Hello World:

REPL

$ node
> console.log('hello world!')
hello world
undefined

Note: undefined is the return value of the previous statement.

-e

$ node -e "console.log('hello world!')"
hello world

File

// index.js
console.log('hello world!')
$ node index.js
hello world

npm

The Node Package Manager (npm), comes with node.js. npm provides the following functionality:

  • Install/publish 3rd-party packages & dependencies

    npm install lodash --save # --save adds the dependency to the package.json 
  • Utility operations for running applications (e.g., npm init, npm start, npm test & npm shrinkwrap)

To learn more, see the Modules & Packages Guide as well as the highly recommended self-paced how-to-npm Nodeschool.io Workshopper.

Next Steps

That's it.

Without covering the specifics of the various core modules or rehashing topics like Filesystem, Control-flow, Modules & Packages, Networking or JavaScript covered already in other guides, there's not much else to cover.

Nodeschool.io

Nodeschool.io is a set of self-paced community built and maintained workshops ("Workshoppers") for learning and building solutions to various common core concepts. It is highly recommended that you run through the following workshoppers:

Core

Recommended Electives

HTTP Frameworks

Consider completing at least one of the following:

Fork me on GitHub