A beginner’s guide to Node.js

Amber Khan
4 min readOct 23, 2020

What is Node js?

Node.js is one of the most widely used technologies today to build API’s that power client end applications. Before this powerful technology hit the computers of developers worldwide, the world of web applications looked very different. In this beginner’s guide I’ll go over why the world needed Node.js, what it does, and how we can use it to build a simple web server.

The basics

Node.js is a runtime environment that allows us to execute JavaScript code without a server. It’s built on Chrome’s V8 JavaScript engine, a super powerful engine that converts our JavaScript code to machine code that a computer can understand. For many years before Node.js came around, Javascript was thought of as a client side language that only ran in the browser. This made it especially fast, but came at a cost as it limited the types of programs it could create, and for a while it was used mostly for specific front end things like user interface widgets and servers. All this changed when Node.js stepped on the scene.

With Node.js in our toolkit, we can now build sophisticated full stack applications with Javascript, and because Node.js uses an event-driven, non-blocking I/O (input/ output) model, we can do this extremely quickly and efficiently. To understand what this means, let’s take a step back itro pre-Node.js times.

Traditional server side technologies, for example, a PHP app ran on an Apache server, work asynchronously, waiting for one process to finish before starting on the next. This is known as blocking, or synchronous architecture. When a request from a client iis received on the server, a thread is allocated to handle that request. That thread can’t be used to serve another client until it has completed it’s database query, or whatever query it is tasked with. Every new request creates a new thread, inevitably leading to all available threads being exhausted, and the client having to wait for a thread to become available.

Node.js, on the other hand, uses non-blocking I/O calls which allow it to sort tens of thousands of simultaneous requests which are held on a single thread, forming what’s called an event loop. Let’s explore what this event loop looks like.

Since Node.js is asynchronous by default, a single thread is used to handle all incoming requests. Let’s say we recieve a request from a client, and we must query a database. If other requests come in, we don’t need to wait for a thread to become available, we can process those requests concurrently. After the database returns with a result of the first request query, it gives an event to the event queue, which is continually being scanned by Node.js in the background. Events that appear in the event queue are immediately pulled and processed. This architecture optimizes scalability in web applications and makes Node.js extremely fast and efficient.

So, what can we build with Node.js?

Node.js is utilized extensively for RESTful APIs. It’s ideal if your application requires the ability to get data from a database and then serve it to a client; for this reason it’s widely used in real time services such as chat applications and games. It shouldn’t be used for CPU intensive applications that don’t operate extensively with file systems or the network.

Cool! How do I install?

Installing Node.js is as simple as heading to the official website, nodejs.org, and downloading one of the pre-built installers for Windows, Mac OS, or Linux.

After you’ve installed Node.js, you’ll have a folder called Node.js in your program files. Within it you’ll find a node.exe file, which is your REPL, Read Eval Print Loop program. We can type Javascript in here and do tons of things, but we can’t use the window, or the document object.

What about NPM?

NPM is a vital component of Node.js. It stands for Node Package Manager (or sometimes Node Package Modules), and it functions to install and manage node modules or programs. It makes it quick and easy to specify and link dependencies. Modules get installed into the “node_modules” folder.

So if we run npm install express, that installs the express module into the node modules folder, and if we don’t have a node modules folder, it’ll create one for us. You can also install modules globally by accessing the -g flag, and that will install it on you system so that you can access it anywhere and not just in that directory or application.

Some popular modules include:

  • Lodash: a JavaScript library of utility functions
  • React: a JavaScript library for building interfaces
  • Moment: parse, manipulate and display dates
  • Express: web development framework
  • Reactdom: React package for working with the DOM
  • Mongo/ Mongoose: Object Data Modeling (ODM) library for MongoDB and Node. js

One last thing: Package.json

The package.json is a super important file used in every node applications or package. It resides in the root of your directory and communicates vital information to npm, such as how your package is structured and what it must do to install it. When you run npm init in your terminal, npm will ask you to input some data about your program and create your package.json file for you, or you can set it manually.

An example package.json file

In conclusion,

Node.js came into existence when the original developers of JavaScript extended it from something you could only run in the browser to something you could run on your machine as a standalone application. Thanks to Node.js, now you can do much more with JavaScript than simply making a site interactive.

--

--