Express.js Fundamentals

Express.js is an amazing framework for Node.js projects and used in a majority of Node-based web apps. Unfortunately, there’s a lack of tutorials and examples on how to write good production-ready code. To help mitigate this need, I released a book called Express.js Guide: The Most Popular Node.js Framework Manual. However, all things start from the basics, and for this post will give you a taste for the basics of the framework so you can decide if you want to continue the learning it further.


Assuming you downloaded and installed Node.js (and npm with it), run this command:

$ sudo npm install -g express@3.4.3


Now we can use command-line interface (CLI) to spawn new Express.js apps:

$ express -c styl expressfun
$ cd expressfun && npm install
$ node app

When this is complete, open your browser to https://localhost:3000.

If you’d like to try a simple Express app, below is the full code of expressfun/app.js (Note: going forward, as of v3.4.3, bodyParser is deprecated):

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));


If you open the expressfun/app.js, you’ll see two routes in the middle:

app.get('/', routes.index);
app.get('/users', user.list);

The first one basically takes care of all the requests to the home page (e.g., https://localhost:3000/) and the latter of requests to /users, such as https://localhost:3000/users. Both of the routes process URLs case insensitively and in a same manner as with trailing slashes.

The request handler itself (index.js in this case) is straightforward: every thing from the HTTP request is in req and you write results to the response in res:

exports.list = function(req, res){
  res.send("respond with a resource");


Each line above the routes in the code above is a middleware:

app.use(express.static(path.join(__dirname, 'public')));

Middleware are a pass thru functions that add something useful to the request as it travels along each of them, for example req.body or req.cookie. For more details on middleware, check out my prior post called Intro to Express.js: Parameters, Error Handling and Other Middleware.


Here is how we define configuration in a typical Express.js app:

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

An ordinary settings involves a name (e.g., views) and a value (e.g., path to the folder where our templates/views live). There is more than one way to define certain settings, for example app.enable for boolean flags.


The Jade template engine is akin to Ruby on Rails’ Haml in the way it uses whitespace and indentation. For example, let’s look at layout.jade:

doctype 5
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    block content

It’s possible to utilize full-blown JavaScript code inside of Jade templates.


Although we’ve barely scratched the surface, hopefully you can see how easy it is to create MVC web apps with Express.js. The framework works great for creating REST API’s as well. If you interested in using express for that purpose, check out my prior tutorials on Tutorial: Node.js and MongoDB JSON REST API server with Mongoskin and Express.js and Intro to Express.js: Simple REST API app with Monk and MongoDB.

If you want to know more about other middlewares and configurations, check out Express.js API docsConnect docs and of course my book — Express.js Guide. For those who already familiar with some basics of Express.js, I recommend going through ExpressWorks, an automated Express.js workshop.

This article was originally published at


Building a Parallax Scrolling Game with Pixi.js

Obey and Report – A JavaScript Event Emitter Pattern