Grokking Node.js
Vote

0% completed

​

What is Node.js?

Running JavaScript Outside the Browser

The Engine Inside Node.js

What People Build with Node.js

How Node.js Handles Many Requests

Other Features of Node.js

A program that runs JavaScript files directly on your computer, without a web browser, is called a JavaScript runtime. Node.js is the most widely used JavaScript runtime. It lets you write web servers, command-line tools and background jobs in the same language that you already use in web pages.

This chapter has three lessons. This one explains what Node.js is and what people build with it. The next two explain how Node.js handles many tasks at the same time, and which parts make up the runtime.

Running JavaScript Outside the Browser

For most of its history, JavaScript ran only inside browsers. A browser loads a web page, finds the scripts in it, and runs them so that the page can react to clicks and fetch data. Node.js removes the browser from that picture: you give it a file, and it runs the file.

Create a file named hello.js that contains one line:

console.log("Hello, Node.js!");

Open a terminal in the same folder and run the file with the node command:

node hello.js
Hello, Node.js!

There is no web page and no browser window. The text goes to the terminal, and the program ends as soon as it has nothing left to do.

The Engine Inside Node.js

Node.js does not run JavaScript by itself. It contains V8, the JavaScript engine that Google built for the Chrome browser, and V8 turns your code into instructions that the processor can execute. The same engine runs your code in Chrome and in Node.js, so the language behaves the same way in both places.

What Node.js adds around V8 is access to the computer. A browser deliberately keeps a web page away from your files and your network ports, because the page comes from a stranger's website. Node.js runs programs that you chose to run, so it gives them modules for reading files, opening network connections and starting other programs. The third lesson of this chapter looks at those parts in detail.

What People Build with Node.js

Web servers and APIs. This is the most common use. A Node.js server receives HTTP requests, reads or writes a database, and sends back HTML or JSON.

Real-time applications. Chat systems, live dashboards and multiplayer games keep a connection open to every user and push updates as they happen. Node.js handles many open connections with little memory per connection, for the reason described in the next lesson.

Command-line tools. Many developer tools are Node.js programs, including npm itself. A script that renames files or checks a project for mistakes is often a few lines of Node.js.

Desktop applications. Frameworks such as Electron combine Node.js with a browser engine, so that a desktop application can have a web interface and full access to the computer.

Small independent services. A large system is sometimes split into small services that each do one job and talk to each other over the network, which is called a microservices architecture. A Node.js process starts quickly and uses little memory, so it fits that style well.

How Node.js Handles Many Requests

Many traditional server platforms give each incoming request its own thread. A thread is a separate line of execution that the operating system can run alongside others, and each one reserves its own memory. A thousand waiting requests then means a thousand threads.

Node.js runs all of your JavaScript on a single main thread instead. When your code starts a slow operation, such as reading a file or querying a database, Node.js hands the waiting to the operating system or to helper threads, and keeps running other code. When the operation finishes, Node.js runs the function that you supplied for its result. The part of Node.js that keeps track of these finished operations and runs their functions one after another is called the event loop.

This design has a cost as well as a benefit. Waiting is cheap, so one process can serve many users who are each waiting for a database or a network reply. Heavy calculation is expensive, because while your code computes on the main thread, nothing else can run. The next lesson measures both effects.

Other Features of Node.js

One language for the whole application. The browser code and the server code can both be JavaScript, so a team can share knowledge, tools and sometimes code between them.

Cross-platform. Node.js runs on Windows, macOS and Linux. Its built-in modules hide most differences between them, such as the character that separates folders in a file path.

The npm package registry. Node.js installers include npm, the Node Package Manager. npm downloads open-source packages from a public registry, so that you can install a web framework or a database driver with one command instead of writing it yourself. You can check that it is installed:

npm --version

Reading Progress

0%


Vote for new content

On This Page

Running JavaScript Outside the Browser

The Engine Inside Node.js

What People Build with Node.js

How Node.js Handles Many Requests

Other Features of Node.js