The Node Beginner Book
About
- The aim of this document is to get you started with developing applications with node.js, teachig you everything you need to about "advanced" JavaScript along the way. It goes way beyond your typical "Hello World" tutorial.
Status
- You are reading the final version of this book, i.e., updateds are only done to correct errors or to reflect changes in new versions of Node.js. It was last updated on 2023-01-06, Version 19.4.0 (Current), by @RafaelGSS (opens in a new tab)
Intended audience
-
This document will probably fit best for readers that have a background similar to my own: experienced with at least one object-oriented language like Ruby, Python, PHP or Java, only little experience with JavaScript, and completely new to Node.js.
-
Aiming at developers that already have experience with other programming languages means that this document won't cover really basic stuff like data types, variables, control structures and the likes. You already need to know about these to understand this document.
-
However, because functions and objects in JavaScript are different from their counterparts in most other languages, these will be explained in more detail.
Structure of this document
-
Upon finishing this document, you will have created a complete web application which allows the users of this application to view web pages and upload files.
-
Which, of course, is not exactly world-changing, but we will go some extra miles and not only create the code that is "just enough" to make these use cases possible, but create a simple, yet complete framework to cleanly separate the different aspects of our application. You will see what I mean in a minute.
-
We will start with looking at how JavaScript development in Node.js is different from JavaScript development in a browser.
-
Next, we will stay with the good old tradition of writing a "Hello World" application, which is a most basic Node.js application that "does" something.
-
Then, we will discuss what kind of "real" application we want to build, dissect the different parts which need to be implemented to assemble this application, and start working on each of these parts step-by-step.
-
As promised, along the way we will learn about some of the more advanced concepts of JavaScript, how to make use of them, and look at why it makes sense to use these concepts instead of those we know from other programming languages.
JavaScript and Node.js
JavaScript and You
-
Before we talk about all the technical stuff, let's take a moment and talk about you and your relationship with JavaScript. This chapter is here to allow you to estimate if reading this document any further makes sense for you.
-
If you are like me, you started with HTML "development" long ago, by writing HTML documents. You came across this funny thing called JavaScript, but you only used it in a very basic way, adding interactivity to your web pages every now and then.
-
What you really wanted was "the real thing", you wanted to know how to build complex web sites - you learned a programming language like PHP, Ruby, Java, and started writing "backend" code.
-
Nevertheless, you kept an eye on JavaScript, you saw that with the introduction of jQuery, Prototype and the likes, things got more advanced in JavaScript land, and that this language really was about more than window.open().
-
However, this was all still frontend stuff, and although it was nice to have jQuery at your disposal whenever you felt like spicing up a web page, at the end of the day you were, at best, a JavaScript user, but not a JavaScript developer.
-
And then came Node.js. JavaScript on the server, how cool is that?
-
You decided that it's about time to check out the old, new JavaScript. But wait, writing Node.js applications is one thing; understanding why they need to be written the way they are written means - understanding JavaScript. And this time for real.
-
Here is the problem: Because JavaScript really lives two, maybe even three lives (the funny little DHTML helper from the mid-90's, the more serious frontend stuff like jQuery and the likes, and now server-side), it's not that easy to find information that helps you to learn JavaScript the "right" way, in order to write Node.js applications in a fashion that makes you feel you are not just using JavaScript, you are actually developing it.
-
Because that's the catch: you already are an experienced developer, you don't want to learn a new technique by just hacking around and mis-using it; you want to be sure that you are approaching it from the right angle.
-
There is, of course, excellent documentation out there. But documentation alone sometimes isn't enough. What is needed is guidance.
-
My goal is to provide a guide for you.
A word of warning
-
There are some really excellent JavaScript people out there. I'm not one of them.
-
I'm really just the guy I talked about in the previous paragraph. I know a thing or two about developing backend web applications, but I'm still new to "real" JavaScript and still new to Node.js. I learned some of the more advanced aspects of JavaScript just recently. I'm not experienced.
-
Which is why this is no "from novice to expert" book. It's more like "from novice to advanced novice".
-
If I don't fail, then this will be the kind of document I wish I had when starting with Node.js.
Server-side JavaScript
-
The first incarnations of JavaScript lived in browsers. But this is just the context. It defines what you can do with the language, but it doesn't say much about what the language itself can do. JavaScript is a "complete" language: you can use it in many contexts and achieve everything with it you can achieve with any other "complete" language.
-
Node.js really is just another context: it allows you to run JavaScript code in the backend, outside a browser.
-
In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does, by making use of Google's V8 VM, the same runtime environment for JavaScript that Google Chrome uses.
-
Plus, Node.js ships with a lot of useful modules, so you don't have to write everything from scratch, like for example something that outputs a string on the console.
-
Thus, Node.js is really two things: a runtime environment and a library.
In order to make use of these, you need to install Node.js. Instead of repeating the process here, I kindly ask you to visit the official installation page (opens in a new tab). Please come back once you are up and running.
"Hello World"
-
Ok, let's just jump in the cold water and write our first Node.js application: "Hello World".
-
Open your favorite editor and create a file called helloworld.js. We want it to write "Hello World" to STDOUT, and here is the code needed to do that:
console.log("Hello World")- Save the file as helloworld.js, and execute it through Node.js
node helloworld.js- This should output Hello World on your terminal.