What is Node.js? A Beginner's Guide

When it comes to coding with JavaScript, Node.js is one of the most exciting technologies around. This is because this mighty runtime environment allows you to use the language to build the backend of websites.

Backend? Runtime environment?

Don’t worry—this guide will give you the lowdown on everything you need to know about Node.js, starting at the start.

Before we get into the topic of Node.js, first let’s clarify what “backend” even means in web development. Next, we’ll explore how JavaScript is employed there, before exploring Node.js in more detail, and why you should use it.

After we check out how it actually works, we’ll look at how the Express framework combines with Node. Lastly, we’ll take a look at some examples of popular websites which employ Node.js.

If there’s a particular section you’d like to skip ahead to, just use the clickable menu:

  1. What is backend development?
  2. Introducing backend JavaScript
  3. What is Node.js?
  4. Why you should use Node.js
  5. How Node.js works under the hood
  6. What is Node.js Express?
  7. Examples of websites built using Node.js
  8. Final thoughts

1. What is backend development?

Backend web development may seem elusive at first, as it concerns all the parts of a website you can’t see. While the frontend is more obvious because you see and interact with it.

A simple way to think about backend development is to see it as the “data access layer”. For any website, you have a database storing all your data. The backend requests this data, sorts it, and sends it to the frontend so it can be visible on the screen.

Another important side of the backend is keeping your website running smoothly. Have you ever tried to book concert tickets, waiting for the right time, and then the website crashed? The website couldn’t handle the higher than usual amount of traffic, and now you’re out of luck. This problem happened in the backend of the website.

Here’s some more examples of backend processes:

  • processing an incoming web page request
  • running a script to show HTML
  • encrypting and decrypting data
  • handling authentication (i.e. is your login info correct?)
  • managing file uploads and downloads

2. Introducing backend JavaScript

When JavaScript was first created, it was something you could only run in the browser. This meant the backend was handled by other languages like Python or Ruby on Rails.

Two different languages means one company needs to find engineers skilled in both. This means more employees and more costs for the company. Wouldn’t it be easier if you could hire one employee for both the front- and backend? Enter JavaScript in the backend and Node.js. This need is why JavaScript now has the capabilities to handle backend responsibilities and is considered a full-stack language.

You can learn more about how the language works on both the front- and backend in our introductory guide to JavaScript.

If you’d like to get stuck into coding some JavaScript for the first time, in this video tutorial our in-house developer Abhi kicks you off:

3. What is Node.js?

Node.js is a runtime environment that allows you to run JavaScript on a standalone machine. A runtime environment is like a small operating system. It provides all the functionality needed for a program to run. JavaScript used to only work in the browser, as browsers contain an engine to interpret JavaScript into code the machine can understand. 

However, the backend doesn’t have this engine that acts as a translator for the language. That’s where Node.js enters the scene. Node.js acts as a JavaScript engine that allows your code to run on a physical machine.

4. Why should you use Node.js

There are many benefits to using this platform, but here are a few main ones:

It’s easy to learn. JavaScript skills are common among developers. Developers who understand the fundamentals of JavaScript can pick up Node.js quickly. This means fewer developers need to be hired.

It’s scalable. Node.js can grow as your applications do, which is important for a business.

It’s cross-platform friendly. Developers can write applications that run across desktop computers and mobile devices. Few modifications are necessary to run on Linux, Windows, and Mac OS.

It’s light and fast. Node.js uses Google’s V8 JavaScript engine and executes code fast.

As I said, these are just some of the reasons this technology is such a success. Node.js fills a need by bridging the gap between the front- and backend of applications. This increases the efficiency of the development process. Technologies such as npm (Node Package Manager) also act as libraries for open-source Node projects.

It’s not hard to see then, why this is just one of the technologies students of our Full-Stack Web Development Program learn as they build out their coding toolkit.

5. How Node.js works under the hood

To understand Node.js, we first must understand that JavaScript is single-threaded. When given a list of instructions, i.e. code, JavaScript executes the code in order via a call stack. Think of a call stack like a list of to-dos for JavaScript to keep track of. It must finish executing one piece of code before moving on to the next. This code can be synchronous or asynchronous.

Synchronous means that each bit of code is handled one piece at a time. A downside of synchronous code is it can cause delays. If a function takes awhile to execute or has to wait on something, it will freeze the whole page up.

Asynchronous code involves the JavaScript engine in our internet browser that we mentioned earlier. JavaScript can hand off tasks to the web API that is part of the web browser’s engine.Once the tasks complete in the browser, the result is pushed to the call stack as a callback. A callback is a set of instructions in the form of a function. Callbacks make sure that a function is not going to run before a task is completed.

  1. You can stop what you’re doing and go to the grocery store, adding a lot more time to the task. 
  2. Or, you could ask your roommate to pick up sugar at the store while you carry on with your work.

The first option is synchronous, the second is asynchronous.

Node.js focuses on addressing expensive I/O (input/output) operations. These are your more time-consuming tasks that may cause delays. Let’s talk about the main concepts involved in Node.js:

Single-threaded 

As we already know, Node.js is based in JavaScript’s single-threaded architecture. This means every time there is a request from the client, it’s handled by one main thread. To go back to our kitchen analogy, that’s you preparing food in the kitchen (the main thread).

Event loop

The event loop allows Node.js to execute normally blocking I/O operations in a non-blocking way. It keeps track of your asynchronous tasks (i.e. your roommate picking up sugar at the store). Once they’re completed, it moves them back to the main thread (you using the sugar in your recipe).

There are six phases of the event loop that are repeated as long as the application still has code to execute:

1. Timers 

This is your setTimeout and setInterval functions. These allow you to executive code after a minimum period of time.

2. Pending callbacks

For example, reading a file using fs.readFile. When your application is waiting to read a file, it can continue to execute code. When the file is ready, it will receive it.

3. Idle, Prepare

Here Node is gathering information and planning what needs to be executed during the next cycle of the event loop.

4. I/O Polling

The phase where all the JavaScript code that we write is executed. This starts at the top of the file and works downwards. Node may execute the code immediately, or add something to the queue to deal with during the next event loop.

5. setImmediate callbacks

Node.js has a special timer setImmediate, which takes priority over all other timers. While the callbacks from setTimeout and setInterval will be out doing their business, setImmediate‘s callbacks will be returned first.

6. Close callbacks

This phase is used when the event loop wraps up one cycle and is ready to move to the next one. It’s used to clean the state of the application. One example of a close event is process.exit(). This will force the process to end even if there are still asynchronous operations waiting to complete.

Tie it together: Single-threaded non-blocking I/O Model & the event loop

Let’s further clarify what non-blocking I/O means, and how it ties together with Node’s event loop.

Input/Output (I/O) is the communication between a computer and the outside world. Input are the signals or instructions sent to the computer. Output are the signals sent out from the computer.

There are many types of I/O sources, including hardware like a mouse or a printer. In the case of Node.js, our I/O sources are networks and file systems.

Node’s I/O operations are non-blocking because of callbacks. When we have an asynchronous function and we’re unsure how long it will take, we send it out via a callback. It gets pushed to the call stack (i.e. a sort of to-do list), then we keep going with the rest of the code (our single thread).

Where does the event loop come in?

The event loop is what allows everything to work together. Node loops through the phases, keeping track of our unfinished asynchronous tasks. The event loop keeps looping and checking on our async operations.

Once a task is complete (i.e. file is loaded), it sends the result back to the main thread. Next time we loop through our I/O polling phase, we execute our callback with the result (i.e. the data from our file). Next, the callback gets popped off our call stack (i.e. check that off our to-do list).

Without the event loop cycling around, we’d send out our async tasks via callbacks and they’d get lost. There’d be nothing there to check back on them. Without the non-blocking I/O model, our application would stall when faced with a time-consuming task. Like being back in the 90s where you just get a blank screen while you wait for ALL the data to be ready.

What is Node.js Express? 

You will often see Express used with Node.js, so it deserves a quick mention. Express is a framework for Node. It provides extra features and makes working with Node simpler. Here are some of the core features of Express:

  • Middleware—These are functions that have access to the request and response objects in Node. Middleware makes your code more compartmentalized and easier to read. You can chain middleware using the next middleware function.
  • Routing—Express allows you to route your request by HTTP method and URL. It helps with readability and organization.
  • Template engine—This allows you to create template files in your application. At runtime, the template engine will replace variables with actual values. This gets turned into an HTML file and sent to the client. It makes it easier to design an HTML page.

Examples of websites built using Node.js

Okay, okay, I get it. Node is cool. But…which companies are actually using it out there in the “real” world?

Have you been on any of these websites recently?

  • LinkedIn
  • Netflix
  • Paypal
  • Medium
  • eBay
  • Trello
  • NASA

Guess what, they’re using Node.js!

Final thoughts

If you think about it from a business perspective you can really see why Node,js is such a game-changer.

Back when JavaScript was first created, it was only used in the browser. Companies had to hire engineers skilled in both JavaScript and backend technologies. Node.js was created to solve this problem. It bridges the gap between front- and backend technologies. Being able to now code full-stack has also predictably led to an increase in JavaScript developer salaries.

Node is also scalable, fast and cross-platform friendly. Its single-threaded architecture allows users to write in JavaScript (also single-threaded). While its event loop provides a solution for executing more time-consuming I/O operations.

Many big companies use Node.js. On top of that, it has great documentation and a big online community, making it a great option for your next project.

If you’d like to learn more about JavaScript and full-stack development in general, check out these articles:

What You Should Do Now

  1. Get a hands-on introduction to web development and build your first website from scratch with our free, self-paced Web Development Short Course.

  2. Take part in one of our FREE live online web development events with industry experts.

  3. Talk to a program advisor to discuss career change and find out what it takes to become a qualified web developer in just 4-7 months—complete with a job guarantee.

  4. This month, apply for the Women in Tech Scholarshipworth up to $1,555 off our Full-Stack Web Development Program. Offered to the first 100 women who enroll, book your advisor call today.

What is CareerFoundry?

CareerFoundry is an online school for people looking to switch to a rewarding career in tech. Select a program, get paired with an expert mentor and tutor, and become a job-ready designer, developer, or analyst from scratch, or your money back.

Learn more about our programs