Title: Getting Started with Express Author: Node Knockout 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.
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 http://expressjs.com/install.sh | sh
Alternatively if we have npm installed we can simply execute:
$ npm install express
First Express Application
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).
$ 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();:
Below is all 8 lines of source we used to create our first Express application: