Think of a server as a server at a restaurant. A customer asks for stuff, and they deliver the stuff. In computing, it’s similar. The server is still called a server, but the customer is called a client. Instead of the stuff being a meatloaf dinner, the stuff is known a service – like sharing data or a resource. The server could be a computer program or a device – it’s not always the giant room of big machines you may be envisioning. Node.js allows you to write computer programs that can function as a server.
Here’s a metaphor. You’re working the register at a sandwich shop. You get an order for a PB&J, and there are 7 more people in line. The synchronous strategy to handle this would be to ask the sandwich-maker to make the PB&J, wait while she makes it, give it to the hungry customer, then take the next order. You keep going this way until everyone in line has a sandwich. The asynchronous strategy would be to pass along the first order, then take another order. Pass along the second order, then take another. At some point, the first PB&J is done and you can deliver it, but you’re taking orders and passing them along to the sandwich-maker the entire time.
Node runs things asynchronously. It gets a request (“i want PBJ.html!”) and lets the computer file system know about the request (“computer #1 wants PBJ.html, can you take care of that?”) Then it’s available to wait for more request while the file system deals with the request – it needs to read it and respond with PBJ.html. Other server frameworks, like PHP or ASP, don’t start listening for additional requests until PBJ.html has been delivered to the requester. They’re the first strategy in the sandwich shop metaphor, while Node.js is the second.
So that’s the asynchronous part. Sometimes it’s also referred to as “blocking” (synchronous, one sandwich at a time) vs. “non-blocking” (asynchronous, multiple orders open at once.)
This is what makes Node.js fast. In event-driven programming, the code only runs when certain events happen. We’re not sending PBJ.html all the time, only when a request is made for it. The events can be things like mouse clicks, pressing a key, or trying to access a certain port (more on that later.) In any case, the code isn’t running unless there’s a reason to run.
So What Does It Do?
Arguably the best parts about Node.js is the Node Package Manager, or npm. It’s the Amazon of web development (you can get anything!) and contains over 600,000 packages that do lots of cool things. You can import these packages into your code and take advantage of their functionality. There are packages to help test your code, to build databases, to allow for real-time communication, to send automated emails to users, and to make front-end development simpler. There are also a number of built-in modules that you don’t have even have to install.
Node.js works best in conjunction with other frameworks from npm – using pure Node.js to built the back-end of a website isn’t great, but using it with Express, Sequelize, React, Redux…now we’re getting somewhere (more on all of those later.)
Next up – Express!