Notes on Node JS

These notes are taken after watching a course by Anthony Alicea. Note: this is an intermediate level course and some of the examples and definitions come straight out of the course.

The first on this series can be found here: Notes on JavaScript: Understanding the weird parts

Lets get to it.

I. The Node Core

V8 JavaScript Engine: is Google’s open source JavaScript engine that turns our JavaScript code to machine code, something the computer processor can understand and follows the ECMAScript standard. V8 is written in the C++ language and is a core part of Node.

Servers and Clients: At the most basic level, a server is just a computer that is performing services that are requested of it from the client. The client requests and the server responds. The Internet runs on this client-server model of computing whereby the browser is the client and the web server is the server.

Node is primarily a server technology. It is designed to let us use JavaScript to write server code. And since JavaScript is also used on the client side, knowing the language allows us to write both on the client(frontend) and server(backend).

Managing a Web Server: The construction of NodeJS means we are trying to answer one important question: What does JavaScript need to manage a server? The features that we need to build a server that JavaScript doesn’t have out of the box but Node provide us with are:

  • Better ways to organize our code into reusable pieces
  • Ways to deal with files
  • Ways to deal with databases
  • The ability to communicate over the internet
  • The ability to accept requests and send responses
  • A way to deal with work that takes a long time

II. Modules, Exports & Require

Module: A reusable block of code whose existence does not accidentally impact other code. Modules organize a program into clusters of code. JavaScript didn’t have this before and this is a new feature in ES6.

Require: is a function that loads and executes a module from a separate file by taking the files path as a parameter. The return value of the module is set by setting module.exports inside the module file. We assign a variable to the return value of the require function in order to use the specific module.

Modules in ES6: Originally Node JS added the module feature to the JavaScript language because the language didn’t have it by default. However, the new version of JavaScript (ES6) now has the module feature builtin. It does the same thing as require in Node, however it has a different syntax, import .


III. Events and Event Emitter

Events: something that has happened in our app that we can listen to and respond to. Events / Event Emitters are a fundamental foundational concept that constitute the majority of core Node JS API. All objects that can emit events inherit their capabilities from the EventEmitter .

Inheriting from the Event Emitter(util.inherits vs ES6 Class extends): Before ES6, we used util.inherits as a way to implement class-like inheritance. ES6 comes with the concept of class that allows us to implement inheritance in a more straightforward manner. However, it should be noted that ES6 Class is just a syntactic sugar over JavaScript’s existing prototype-based inheritance and should not be equated with a classic object-oriented inheritance.

  • Inheriting using util.inherits
  • Inheriting using ES6 Class extends

IV Asynchronous Code

Asynchronous: means more than one process running simultaneously. Node is asynchronous. However V8 and JavaScript are synchronous — one process executing at a time. This means that even though JS is synchronous, where its being run from (inside the browser, Node..) can make it perform asynchronously without blocking other code from running.

Callback: A function passed to some other function, which we assume will be invoked at some point. The function ‘calls back’ invoking the function you give it when it is done doing its work.

LIBUV and the Event Loop: As we have seen before custom events can be handled with the Event Emitter, which is JavaScript based and exists in Node. However, system events are handled in the C++ side of the Node core which are actually handled by a C library called libUV. Unlike the Event Emitter, libUV manages events coming from the operating system itself.

Event Driven, Non-Blocking I/O in V8 JavaScript. Image by Anthony Alicea

Event Driven, Non-Blocking I/O: We’ve already seen that inside Node, V8 is synchronous and runs one set of code at a time. Also inside Node is the libUV library, which deals with events occurring in the operating system.

  • From the diagram above we can see that libUV connects by requesting something(e.g. open a file) from the OS.
  • After some time (while v8 is still running without blocking) the OS sends back an event to satisfy libUV’s request.
  • The event is then placed inside the queue, which holds all the events that have been completed.
  • Then the Event Loop is constantly checking if something has happened inside the queue.
  • When the event loop finds an event that is completed, it process it and sends a callback.
  • Then V8 will run the callback ones the currently running code is done because it is synchronous. However, the entire process is asynchronous.

Buffer: A temporary holding spot for data being moved from one place to another. It is intentionally limited in size. Generally this data is coming and moving through a Stream. A buffer ultimately holds raw binary data. As such it’s not very often that we will directly deal with buffers. Most times buffers are something that’s coming back from some other utility.

Stream: A sequence of data made available over time (instead of trying to get the whole data at once). The pieces of data eventually combine into a whole. For e.g. watching a movie stream over the internet instead of downloading the full movie.

A Buffer(chunk) of data moving along a Stream. Image by Anthony Alicea

Pipes: Connecting two streams by writing to one stream what is being read from another stream. In Node we pipe from a Readable Stream to a Writable Stream. Since the pipe method returns the destination stream we can chain another pipe method call on the destination stream (now a readable stream) and stream into another writable stream.

V. HTTP Webserver

Protocol: A set of rules two sides agree on to use when communicating. Both the client and server are programmed to understand and use that particular set of rules.

TCP/IP: in the Client-Server model each computer is assigned a unique IP (Internet Protocol) address in order for the client and server identify each other. After identifying each other the client and server then open a socket connection between them. We send information across this socket connection. The information we are sending is split into small packets of data and sent across the socket from one computer to another. The act/way of splitting this information into small packets and sending them across the socket is called TCP ( Transmission Control Protocol). The individual packet of information may be defined by other protocols like HTTP, FTP, SMTP and others. The operating system has a TCP/IP capability and Node then provides us with the ability to access those features.

TCP/IP Image by Anthony Alicea

Port: This is how, once a computer receives a packet, knows what program to send it to. When a program is setup on the operating system to receive packets from a particular port, it is said that the program is ‘listening’ to that port. For e.g. a computer with an IP address of and running a NodeJs program listening at port 443 will have a complete socket address of . Obviously we usually assign a domain which maps to the socket address like

HyperText Transfer Protocol (HTTP): A set of rules (and a format) for a packet of data being transferred on the web via the TCP/IP protocols. For e.g.

HTTP Methods (Verbs): Specifies the type of action the specific request wishes to make inside the request itself. GET, POST, DELETE, and others.

Application Programming Interface (API): A set of tools available to us for building a software application. On the web the tools are usually made available via a set of URLs which accept and send only data via HTTP and TCP/IP. In other words, the URL instead of returning a web application it responds back with data. We can also send data to that URL as part of our HTTP request and the API will do something with that data.

Representational State Transfer(REST): An architectural style for building APIs. It means that when we handle HTTP requests we decide that the verbs and the URLs mean something. Having a RESTFul API means you design your API so that it responds to the HTTP request verbs in the way that it is expected. It standardizes the way of using the HTTP request so that when they’re sent they match what’s actually happening.

VI Express

Express: is a minimalist web framework for Node.js

Middleware: code that sits between two layers of software. In the case of Express, sitting between the request and the response.

Static files: files that are not processed by code in a any way. HTML, CSS, and image files are ‘static’ files.They are just uploaded to the server and are just sitting there.