This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
Foreword
Dear reader,
You are reading a book which will lead you to the understanding and fluent use of the Express.js framework the de facto standard in web application programming on Node.js. I would especially recommend this book because it was written by a practicing engineer, who has comprehensive knowledge of the full stack of web application development, and Express.js in particular.
Azat and I worked on the same Node.js/Express.js code base at Storify the social media curation tool that The Washington Post, CNN, BBC, The White House press corps and other news corps use. It was recently acquired by LiveFyre. Right before the Express.js Guides release, Azat asked me to write the foreword, because it will sound objective, sincere and unbiased coming from the creator of another Node.js framework: CompoundJS.
However, nobody is reading forewords. So, instead of a foreword, Ill share my story. Actually, I never thought it was worth sharing and theres definitely nothing exciting about it. But from the other point of view thousands of young programmers living similar ordinary lives - it could be inspiring: its a common story, but a kind of successful one.
My path to web development started when I was a student. Id joined a team as a junior PHP programmer. I was working here for about five years, and the main lesson I have learned was that education is nothing compared to real work experience.
The next page of my professional life was my work in outsourcing (PHP and Ruby on Rails). And then I found Node.js. It was something that I always wanted: processes that do not have to wait for DB/IO operations, which are keeping all the resources, but doing something useful instead. Thats why I started using it its more efficient compared to synchronous programming environments. By efficient I dont mean speed of processing, but rather more flexibility in programming style.
As a good example of its flexibility, I can share some solutions I recently programmed for a Redis adapter for the Jugglingdb ORM. The problem: during peaks in website usage, we are running a lot of db queries to serve pages, and most of the queries are the same. The obvious solution is to cache results of the queries, but this solution requires additional coding and some logic for cache invalidation.
Weve come up with a better solution: cache queries and not results. When a query comes, we dont execute it immediately; instead, we wait for some time, collect identical queries, then execute the query once and run multiple callbacks to serve all clients. This solution is simple and requires no additional logic. As a result, we have flat db usage, even during peaks. This solution is natural in Node.js, and thats why Node.js rocks!
Life after discovering Node.js was great, full of interesting challenges and work, but one thing was annoying: each time I start a new project, I have to do almost the same work to organize code. For me, as a Rails developer, its really great to be able to create well-structured MVC applications fast, generate scaffolding controllers/views and other stuff. But this kind of tool was missing in Node.js and thats why I spent my Christmas holidays writing it. The project was called express-on-railway at first, then RailwayJS, then CompoundJS.
The main goal of the project was to bring structure to an Express.js application, add the ability to extend applications in a standard way, and generate application code. So, it was not a new framework, but just Express.js with decent MVC structure, which is good for developers who dont need to learn anything but Express.js to be able to understand whats going on in a CompoundJS application. And it was a kind of piggybacking on Express.js and Rails experience. The idea was to get the best ideas from Rails and bring them to the node platform, and Express.js was selected as the base because it is the most popular framework for Node.js, and it has a relatively big community, so I wouldnt be alone with my new framework. It was the start of my open-source years, which completely changed my attitude toward programming (and other life matters), but thats another story.
And what can I say to conclude: web development in Node.js started with Express.js. It is a minimalistic and robust framework that gives you all you need to build decent web applications. Even if you decide to move to some more advanced frameworks at some point, Express.js knowledge is still a basic skill you have to learn. In addition, this book contains everything you need to know to start using Express.js. It clearly explains all concepts, as well as the answers to the most frequent questions that newcomers ask. For these reasons, this book is a must-read!
Anatoliy Chakkaev,
Creator of CompoundJS and JugglingDB
Acknowledgment
This book would not be possible without the existence of my parents, the Internet, and JavaScript. Also, words cannot express my gratitude to Ryan Dahl and TJ Holowaychuk.
In addition to that, special thanks to General Assembly, pariSOMA and Marakana for giving me the opportunities to test my instructions out in the wild; to Peter Armstrong for LeanPub; to Sahil Lavingia for Gumroad; to Daring Fireball for Markdown; to Metaclassy for Byword; to Fred Zirdung for advice; and to Rachmad Adv for the splendid cover!
Introduction
Summary
In this part, well go over why this book was written, who might benefit from the book and how to use it most efficiently.
Why
Express.js is the most popular Node.js web framework yet. As of this writing (September of 2013), there are no books solely dedicated to it. Its official website has bits of insights for advanced Node.js programmers. However, Ive found that many people including those who go through the HackReactor program and come to my Node.js classes at General Assembly and pariSOMA are interested in a definitive manual, one that would cover how all the different components of Express.js work together in real life. The goal of The Express.js Guide is to be that resource.
What This Book Is
Express.js Guide is an exhaustive book on one particular library. There are four distinct parts:
- A hands-on quick start walkthrough to get a taste of the framework
- An Express.js API 3.3.5 description which can be used as a reference
- The best practices for code organization and patterns
- Tutorials (meticulously depicted coding exercises) and examples (less detailed explanations of more complex apps) for the real world.
The Express.js Guide covers middleware, command-line interface and scaffolding, rendering templates, extracting params from dynamic URLs, parsing payloads and cookies, managing authentication with sessions, error handling, and prepping apps for production.