SitePoint
  • Premium
  • Library
  • Community
  • Jobs
  • Blog
LoginStart Free Trial
Node.js: Novice to Ninja
Node.js: Novice to Ninja
Notice of Rights
Notice of Liability
Trademark Notice
About Craig Buckler
About SitePoint
Why Learn Node.js?
Summary
Quiz
Choosing a Node.js Version
How to Install Node.js on Linux (or Windows WSL2)
How to Install Node.js on macOS
How to Install Node.js on Windows
How to Install Node.js on Other Devices
Run JavaScript Commands in the Node.js REPL
Summary
Quiz
Your First Console App
Restarting Node.js Applications with Nodemon
What is Debugging?
How to Avoid Bugs
Node.js Debugging Environment Variables
Node.js Debugging Command-line Options
Console Debugging
Node.js util.debuglog
Debugging with Logging Modules
Node.js V8 Inspector
Debugging Node.js Apps with Chrome
Other Node.js Debugging Tools
Exercise: Debugging webhello.js
Summary
Quiz
Why use Express?
Create a New Node.js Project
Switch to ES6 Modules
Install Express
Create the Express Entry Script
Processing HTTP GET Query Strings
Global vs Local Packages
npm Help
npm Configuration
Project Initialization
Searching for Packages
Installing Packages
“No-install” Execution
Listing Packages
Finding Outdated Packages
Removing Packages
Using npm Scripts
Publishing Packages
Exercises
Summary
Quiz
CommonJS
ES2015 Modules (ESM)
Comparison of CommonJS and ES2015 Modules
Importing CommonJS Modules in ES2015
Requiring ES2015 Modules in CommonJS
Using ES2015 Modules in Browsers
Summary
Quiz
Single-threaded Non-blocking I/O Event-looping What?
The Event Loop
Callback Conundrums
Promises
async / await
Exercises
Summary
Quiz
A Database-driven Web Application Example
MongoDB
MySQL
Sequelize ORM
How to Choose the Right Database
Exercises
Summary
Quiz
What Are WebSockets?
Example WebSocket Chat Application
WebSocket Walkthrough
Advanced WebSockets Considerations
Exercise
Summary
Quiz
Process
OS
Util
URL
File System
Events
Streams
Worker Threads
Child Processes
Clusters
Exercises
Summary
Quiz
Source Code
Quizzing Quick Start
Summary
Why Develop Using Multiple Servers?
1. One PostgreSQL Database Server
2. Two Express HTTP Web Servers
3. Three WebSocket Servers
4. One Traefic Load Balancer
5. Adminer Database Client
Docker Development Environment
Docker Production Environment
Summary
Database Library
Question Database Initialization
Starting a New Game
Joining a Game
Quiz Page
Summary
Initiating a WebSocket Connection
Game Logic
Exercises
Summary
Perfect Package Pursuit
Development Tools
Testing
Logging
Full-stack Frameworks
Server-side Frameworks
Web Publishing, Content Management Systems, and Blogging
Headless Content Management Systems
Static Site Generators
Database Drivers
Templating
Command Line
File System
Network
WebSockets
Images
Email
Security and Authentication
Summary
Pages vs Applications
Node.js Application Preparation
Dedicated Server Hosting
Static Site Hosting (Jamstack)
Serverless/Lambda Functions
Container Hosting
Summary
Is Node.js for You?
Is Deno Better?
Thank You for Reading!

What is Node.js?

Node.js is a JavaScript runtime, which means it runs programs written in JavaScript. Most developers use it to create command-line tools or web server applications.

Skip Ahead?

That’s everything you need know about Node.js. If you’re eager to start programming, skip ahead to Chapter 2. That said, it’s worth revisiting this chapter later to learn about Node’s advantages and core features.

JavaScript, JScript, ECMAScript, ES6, ES2015?

To make learning more confusing for beginners, JavaScript has many names. It started life as “Live Script” in 1994. Netscape rebranded it as “JavaScript” following a hasty deal with Sun Microsystems, despite it bearing little resemblance to Java or lightweight scripting. Microsoft couldn’t use that name, so it became JScript in Internet Explorer.

In 2005, Mozilla (which grew out of Netscape) joined ECMA International and standardized the language as “ ECMAScript ”. Versions 1 to 3 documented the evolution of JavaScript up until 1999. Version 4 was abandoned, but ECMAScript 5 became the standard supported by most browsers in 2009.

Work then started on ECMAScript 6—or “ES6”. The final specification was approved in 2015, which led to yet another name: “ES2015”. New specifications now arrive every year.

Rightly or wrongly, this course refers to “JavaScript” throughout. References to specific versions (such as ES9/ES2018) are only made if they affect the version of Node.js you need to use.

Node.js was initially developed by Ryan Dahl. He took the V8 JavaScript engine from Google’s Chrome browser, added some APIs, wrapped it in an event loop, and launched it as an open-source product on Linux and macOS in 2009. The Windows edition arrived in 2011.

The Node Package Manager (npm) was introduced in 2010. It allowed developers to use code modules published by others in their own projects. There was no official ECMAScript module standard at the time, so Node.js and npm adopted CommonJS.

The first (non-beta) release of Node.js arrived in 2015, with updates promised every six months.

Node.js wasn’t the first JavaScript runtime, but unlike other options—such as Rhino and SpiderMonkey —its popularity grew exponentially. Even those writing PHP, Python, Ruby or other languages often use Node.js tools to supplement their development processes.

Why Learn Node.js?

JavaScript is hugely popular on GitHub, and it’s ranked highly by developers. Companies including Netflix, Uber, Trello, PayPal, LinkedIn, eBay, NASA and Medium have adopted Node.js, and most professional developers will have encountered Node.js tools.

Below, we’ll look at some of the reasons you should consider using Node.js.

It’s JavaScript

JavaScript is used on trillions of web pages, where it has a browser monopoly. Every professional web developer requires JavaScript knowledge to program client-side applications.

Server-side languages are more diverse. Historically, developers could opt for PHP, Ruby, Python, C# (ASP.NET), Perl, or Java, but these have different syntaxes and concepts. It can be difficult to switch contexts, so larger project teams often split into frontend and backend developers.

Node.js allows a developer with frontend JavaScript knowledge to leverage their skills on the backend. It won’t make you a full-stack developer overnight, but the concepts are similar, and there’s less rigmarole when formatting JSON, handling character sets, using WebSockets, and so on.

JavaScript Alternatives

Some developers prefer languages such as TypeScript, PureScript, CoffeeScript, Reason, and Dart, which can transpile to JavaScript and run in a browser or Node.js. Ultimately, it still results in JavaScript code.

It’s Fast

Most server-side languages are fast enough, but few match the speed of Node.js. The V8 engine is quick, and it evolves rapidly, having the weight of Google and Chrome behind its development. Node.js also has a non-blocking, event-driven I/O.

Let’s go through that again with less jargon. Most languages use synchronous blocking execution. When you issue a command—such as fetching information from a database—that command will halt further processing and complete before the runtime progresses to the next statement. To ensure that multiple users can have access at the same time, web servers such as Apache create a new processing thread for every request. This is an expensive operation, and Apache has a default limit of 150 concurrent connections. Busy servers can become overloaded.

Node.js code (and browser JavaScript) runs on a single processing thread. Long-running tasks such as a database query are processed asynchronously, which doesn’t halt execution. The task runs in the background, and Node.js continues to the next command. When the task is complete, the returned data is passed to a callback function. A Node.js program can have many hundreds of ongoing operations that are completed whenever they’re finished, meaning that the processor is free to tackle other tasks.

Asynchronous programming has challenges, but it’s possible to create fast Node.js applications that scale well.

It’s Real-time

Web platform features such as WebSockets and server-sent events permit real-time functionality—such as instant data updates, live chat, multiplayer games, and more. These can be difficult to implement in traditional server-side languages, where they often require third-party services. Real-time functionality in Node.js is significantly easier.

It’s Lightweight

The Node.js runtime is small and cross-platform. As well as catering for Linux, macOS, and Windows, you find editions for embedded systems, the Raspberry Pi, and even SpaceX rockets.

It’s Modular

Node offers a minimal standard library with good documentation. It contains basic functions for error handling, file system access, network operations, and logging.

For everything else …

It’s Extendible

Node.js has the largest package registry in the world, with more than one million modules. You’ll find pre-written code for task runners, loggers, database connectors, image processors, code compilers, web servers, API managers, and even client-side libraries.

The npm command-line tool is provided with Node.js and makes it easy to install, update, and remove modules. You can also use it to install global modules so Node.js scripts can run as commands from anywhere on your system.

It’s Open Source

Node.js is an open-source project. The runtime is free to use without any commercial restrictions. The majority of modules are also free, because they’re submitted by the community for the benefit of other developers.

It’s Everywhere

This course concentrates on web applications, but you can use Node.js to create serverless functions, deployment scripts, cross-platform command-line tools, and even complex graphical apps such as VS Code, Slack, and Skype—all of which use the Electron framework.

As a web developer, you’ll almost certainly encounter Node.js, even if it’s not a core part of your technology stack. Knowing a little Node.js could help your projects and career. You’ll have a better insight into the possibilities available to modern web applications.

What About Deno?

Node.js is a cross-platform, V8-based JavaScript runtime released by Ryan Dahl in 2009.

Deno is a cross-platform, V8-based JavaScript runtime released by Ryan Dahl in 2020.

Deno smooths over some cracks and inconsistencies of Node.js, with the benefit of a decade’s worth of hindsight. It directly supports TypeScript without a compiler, uses ES6 modules rather than CommonJS, replicates many browser APIs (window, addEventListener, Fetch, Web Workers, etc.), and provides built-in tools for linting, testing, and bundling.

Deno is great—but it’s new, and yet to achieve a fraction of Node’s popularity. The frameworks are similar: if you know one, it’s easy to switch to the other.

Summary

In this chapter, you’ve learned that Node.js is a popular JavaScript runtime that’s uniquely suited to web development. I’ve summarized it in this chapter’s video. Chapter 2 describes how to install Node.js on your platform of choice.

Quiz

Many chapters in this course end with a quick quiz to ensure you’ve grasped the concepts. Beware! Some questions are designed to catch you out, so make sure you’ve been paying attention!

End of PreviewSign Up to unlock the rest of this title.

Community Questions