Get a better understanding of how Node.js works by imagining it as two different types of restaurants. In this tutorial, Alex Banks will first explain how Apache works, then demonstrate how Node.js is different due to it being single-threaded. You will also learn about nonblocking, event-driven IO, and asynchronous activity.
- I'm going to demonstrate how Node.js works and why it is so fast by taking a look at two restaurants. The first restaurant is Apache Steaks and Chops. It is a big, nice, fancy restaurant. In this restaurant, every new guest represents a new user, and making an order is like making a request. If I place an order for a salad, the manager will need to hire a new waiter to take care of me. In this restaurant, our waiter represents a thread. We are going to have our own waiter, our own thread, and they will handle all of our orders. This is similar to how Apache works.
Every request is single-threaded. After placing the order, the waiter will take the order to the kitchen and give it to the chef. And now the waiter just waits. He won't do anything else until the chef is finished making the food. I would like to order a glass of water, but I can't order anything until the chef finishes making that salad. The chef is blocking me from being able to simply order a glass of water. In this analogy, the chef represents the file system or a data store. In Apache, the single thread waits for the file system to finish reading files before it can do anything else.
We refer to this as blocking. Finally, my salad is ready. My waiter brings me the food. I can order my glass of water, and my waiter also brings me that, too. My request has been served. And now the manager is firing my waiter because they are not needed anymore. Now, when this restaurant gets busy for dinner service, every guest has their own waiter, which is pretty nice. That is pretty good service, but the waiters are mostly hanging around the kitchen and waiting for the chef to make the food. If this restaurant gets really popular, it requires a lot of space to expand because more guests means more waiters.
Now, let's take a look at this other cafe, Chez Node. At this cafe, there is only one waiter because Node.js is single-threaded. Here, we can order some crepes. We can see that our waiter places the order for the food, then moves on to take an order from another new table. Hmm, this single thread services all of the restaurant guests. That is pretty cool. When my crepes are ready, the chef rings a bell, and our waiter goes and gets the crepes and delivers them to me. He then proceeds to take another order from a new table. When their food is ready, the waiter will bring it to them as soon as he can.
We can say that this waiter behaves asynchronously. Everything this waiter needs to do represents a new event, a new table, placing orders, delivering orders. These are all events, and they will get handled in the order that they are raised. Our waiter does not wait. There is no blocking. Our single waiter is busy, busy, busy, but he is killing it because he can multitask. This is what it means when we say nonblocking, event-driven IO. We have a single thread that will respond to events in the order that they are raised.
This thread behaves asynchronously because it does not have to wait for resources to finish doing what they're doing before our thread can do anything else. If this cafe gets popular, we can simply franchise it. Chez Node can easily be expanded by simply duplicating or forking the restaurant into a neighboring space. And this is precisely how we host Node.js applications in the cloud. Now, remember, Node.js is single-threaded. All of the users are sharing the same thread. Events are raised and recorded in an event queue and then handled in the order that they were raised.
Node.js is asynchronous, which means that it can do more than one thing at a time. This ability to multitask is what makes Node.js so fast and one of the reasons so many developers are building their web applications with Node.js.
Alex Banks shows how to install Node.js on a Mac or PC and work with the Node.js core: the global object, event loop, http module, and file system. Then he covers reading and writing data, streaming data, making http requests, and working with the node package manager (npm). In the final chapters, he shows how to start sessions on web servers, communicate with web servers and clients with WebSockets, use the Express framework to develop applications, and test and debug Node.js code. Want to speed up your Node.js workflow? Check out the tutorials on automating tasks with Grunt and npm scripts.
- What is Node.js?
- Installing Node.js
- Understanding the global object and global timing
- Importing the core modules
- Handling events
- Creating child processes
- Reading, writing, and removing files
- Working with file streams
- Making http requests
- Serving files and JSON data
- Installing npm, the node package manager
- Working with Express, the web server framework
- Using WebSockets
- Testing and debugging Node.js code
- Working with Grunt and Browserify
- Automating tasks with npm scripts
Skill Level Intermediate
Q: This course was updated on 09/06/2017. What changed?
A: New videos were added that cover upgrading Node. In addition, the following topic was updated: debugging with npm.