Nodejs Book (Abstract)

Forward

In 2017 I wrote a small “book” about getting started with Nodejs as an HTTP server. The book has been sitting on my hard drive for the last three years, so I figured if there is nothing better to do with it, then I might as well post it to this blog. To post a bit of background to this book, I should probably explain some of my experience with Nodejs. In 2011 was a student in university getting into server-side programming and I was having trouble of managing the differences between client-side Javascript and server-side PHP. That’s when the first video with Ryan Dahl came out announcing Nodejs.

With Nodejs having just come out, there really wasn’t a lot to go on, and I watched and re-watched this video over and over again to try and understand what was going on. When Nodejs was first announced it was marketed as “nodejs on the server”, and me and I think a lot of other people thought it would be the Javascript version of PHP where we could define a file that would be executed on the server. Instead what we were greeted with was “http.createServer”, and then the realization hit me. We weren’t running Javascript on the server, as much as we were using to define how the server should run.

Shortly after the release of Nodejs came the release of Express, and I had a few friends who started using Express and being able to easily use Nodejs even though they personally admitted they had no idea what was going on. I resisted the temptation to start using Express, as I didn’t like the idea of a “black box” that takes care of everything for you. I really wanted to understand the fundamental mechanics of Nodejs, and what was going on, and then deciding on whether to use something like Express from there. As a side note, after putting in the investment I do use Express, and enjoy it a lot. But I still appreciate the effort I put into trying to learn Nodejs “from the ground up”, and that’s what I hope to share in this “book”.

As a note, this book has not been through an editor so there could be potential mistakes. Since we have comments disabled on this site, if you would like provide feedback, you can tweet me over at @WsdCollins, with a link to the page and you’re comment. And I can go ahead and add the tweet into a given page along with the correction.

Abstract

Nodejs is often simply refered to as “server side Javascript”. However this definition can be a misleading. Nodejs can be more accurately defined as a Javascript run time with built in modules for networking. What this means is the text files we write to define a Nodejs program are interpreted by Google’s V8 Javascript engine and executed by the computer’s hardware. Nodejs, at it’s core has several built-in modules for the file system, tcp and udp networking, as well as handling buffers and processes. In this respect Nodejs is a networking toolkit which we can use to define applications in Javascript that interact with other computers on a network. An http or https based web server is one of the many types of networked based applications that can be defined with Nodejs.

One of the caveats of creating a web server in Nodejs is that we define a process which defines the web server itself, along with the logic that runs on top of it. This concept is not unique to Nodejs in the least, but it does contrast with other languages such as PHP where either Apache or Nginx serves as a web server, and PHP is only run on files ending with the file extension of “.php”. Because we are defining a process to act as a web server, and implement logic on top of it, that it is extremely beneficial when working with Nodejs to understand the fundamental concepts and standards that the web is based off of.

In this book we will create functional web server using Nodejs and describe the web standards and background knowledge for each concept of implementing our web server.