This is because this mighty runtime environment allows you to use the language to build the backend of websites. It’s no surprise, then, that Node is being used by one out of every two developers, according to the 2022 Jamstack Community Survey.
But wait—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.
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:
- What is backend development?
- What is Node.js?
- Why you should use Node.js
- How Node.js works under the hood
- What is Node.js Express?
- Examples of websites built using Node.js
- 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
3. What is Node.js?
4. Why should you use Node.js
There are many benefits to using this platform, but here are a few main ones:
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.
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
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.
- You can stop what you’re doing and go to the grocery store, adding a lot more time to the task.
- 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:
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:
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
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?
Guess what, they’re using Node.js!
If you think about it from a business perspective you can really see why Node,js is such a game-changer.
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.