Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
106 lines (78 sloc) 3.73 KB

Title: Getting Started with Express Author: TJ Holowaychuk Date: Sat, 11 Sep 2010 22:55:36 GMT Node: v0.2.1

This was the second in a series of posts leading up to Node.js Knockout on how to use node.js. This post was written by guest author and Node.js Knockout judge Tj Holowaychuk and is cross-posted on his blog.

In this short tutorial for Node Knockout we will be creating a small application using the popular Express framework.

Express is a light-weight Sinatra-inspired web development framework. Express provides several great features such as an intuitive view system, robust routing, an executable for generating applications and much more.


To get started with Express we first have to install it. There are several ways to do so, however my personal favourite is the following command which does not require a node package management system:

$ curl | sh

Alternatively if we have npm installed we can simply execute:

$ npm install express

First Express Application

To create our first application we could use express(1) to generate an app for us, however an Express app can be a single JavaScript file if we wish, and in our case of a simple “Hello World” app that is exactly what we will do.

The first thing we need to do is require express, and create an app. The app variable shown below is an express.Server, however by convention we typically refer to Express servers as “apps”.


Our next task is to set up one or more routes. A route consists of a path (string or regexp), callback function, and HTTP method. Our hello world example calls app.get() which represents the HTTP GET method, with the path “/”, representing our “root” page, followed by the callback function.


Next we need our server to listen on a given port. Below we call listen() which attempts to bind the server to port 3000 by default, however this can be whatever you like, for example listen(80).


We can execute the app simply by executing node(1) against our JavaScript file:

$ node server.js
Express server started on port 3000

Finally to confirm everything is working as expected:

$ curl http://localhost:3000
Hello World


Behind the scenes the Connect middleware framework developed by myself (TJ Holowaychuk) and Tim Caswell is utilized to power the Express middleware. For example if we wish to add logging support to our hello world application, we can add the following line below app = express.createServer();:


For more information on middleware usage view the Middleware section of the Express Guide.


Below is all 8 lines of source we used to create our first Express application: