Node.js 101

Node.js is a server environment.  It’s open source, free, runs on a bunch of different platforms, and uses JavaScript.  It works well because it’s asynchronous and event based!

Server Environment?

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.)

Uhh…Event Based?

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?

Node can generate the content on a web page, it can manipulate files, it can play nicely with databases (add, delete, or modify info), or it can collect data from a form.  Cool!  Even cooler – it’s just a JavaScript file.  The file has instructions on what to do when certain events happen.

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!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s