A Beginner’s Tutorial for Node.js
The goal of this tutorial is to bring a beginner fully up to speed with Node. At the end you will understand Node’s eventing model and be able to create a site using the most popular modules and frameworks. It should take about 4 to 5 hours total.
I’ve found that the most important thing for understanding Node is to realize it initially places an additional burden on the programmer. In traditional web frameworks such as Rails or Django, you typically write methods to handle incoming requests. Your method can call directly to the database or file system because it runs on its own thread — it’s allowed to take as long as it wants to return a response. The server can always spin up other threads to handle other incoming requests. This makes the logic straightforward — the entire handler can be written in one method.
In Node, the entire server has one single thread, so now your code must play nice. If any of your handlers call a blocking method, the entire server is blocked. Your code has to be constructed so that it always returns immediately and is called again when a response from the database or filesystem is ready. The difference from before is that the handler logic is split. Understanding this difference is the key to understanding Node. I mention this here because initially most Node tutorials gloss over this point. Just keep it in mind. It will become more clear as you start learning.
The Basics (1 – 2 hours)
Start with ‘The Node Beginner Book’ by Manuel Kiessling. For me it was the best overall getting-started guide to Node.js . It can be read online for free. Read the book and follow along with the examples. It’s a short, easy to read book. The last section, on handling file uploads, can be skipped.
It also has instructions and a link to installing Node on your machine. If you’re on Windows you can also find instructions here.
By the end you’ll know how to set up a server on your machine, create a simple web server, use modules to organize code into files, create a simple routing system for requests, and handle POST requests. You should understand Node well enough to learn about some existing libraries and tools.
Bonus reading: Look at chapter 3 of ‘Up and Running with Node.js’ for a more in-depth discussion of the event loop and why blocking it is bad.
Going Deeper (1 hour)
Now that you know the basics of Node it’s time to go a bit more in-depth. We’ll start by taking a closer look at callbacks and control flow. Read chapter 7 of ‘Mixu’s Node Book’. Optional: continue to chapter 8 for a review of modules.
When you’re comfortable with control flow read chapters 9, 10, and 11 to learn about the core API you’ll be using in all Node programs. You can skim these a bit, since the book goes into detail, but just be aware of the APIs available so you know to look them up later.
One thing you’ll notice missing from Node are web framework niceties like a view / templating system. Finding a Node web framework is the next step.
The Express Web Framework (45 min)
To continue we’ll take a look at Express.
Express is currently the de-facto web framework for Node. Start by watching nodetuts.com‘s series of videos on Express: episode 9, 10, and 11. They’re about 15 minutes each and are a good intro to Express and the Jade templating language.
A Real Example (1 hour)
Time to look at a real example. Read this blog post and follow along. It will show you how to create a simple blog site using Express as a web framework, Jade and Stylus for templating, and MongoDB for the backend.
Before you get started it might be helpful to take a quick tour of MongoDB. It’s an interactive tutorial that takes only a few minutes to complete and helps provide context for the lesson.
Congrats! You should now be able to create a ‘real’ database-backed web site using Node. More importantly, you’ve learned enough to be able to continue learning on your own.
For this course I’ve tried to bring together the best content in order. Any better resources out there that I forgot? Please mention it in the comments!
List of resources mentioned in order
- Node Beginner Book
- Up and Running with Node.js
- Blog rolling with mongoDB, express and Node.js from howtonode.org
Chapters 5, 6, and 7 of Up and Running with Node.js have an overview of the Node.js core API.
Node Web Development is a decent overall introduction to Node.
The official Node documentation has a list of useful Node modules.