Getting started using Node.js and Express.js to build a simple website

Post to Twitter

The community around Node.JS never ceases to amaze me how fast it has grown since Node.JS was first introduced not that long ago. I’ve seen many projects take off and one in particular has caught my eye: Express.JS which as their websites states is “High performance, high class web development for Node.js”. Today I’ll show you how to get Express up and running.


First you will need to have Node.JS installed. You can check out an earlier article of mine that shows you how to do that under Ubuntu 9.10. I am currently running Node.js under Ubuntu 10.04 on VMWare Player hosted on a Windows 7 box.

Once Node is installed and working you can use Git to setup Express. In my home folder I created a folder called “repository” which is where I installed Node at. This is also where I cloned Express into using the Terminal like so:

git clone git://github.com/visionmedia/express.git
cd express && git submodule update --init

This will setup a folder called “Express”. I then created another folder called “express-demo”, this is where I put my sample/demo code for working with Express. So my folder structure looks like this:

Lets try one of the examples that ships with Express. The “Chat” example seems like a fun place to start but before we can run it you need to make a small change to the app.js file in the Chat folder (assuming your folder layout is like mine).

Change the first line in app.js from:

require.paths.unshift('lib')

To:

require.paths.unshift('../../../express/lib')

Now navigate to the chat folder in your shell and run the chat program.

node app.js

Fire up two browsers and point them to the default address http://localhost:3000/ and you should see a simple chat program come up. Try it out.

Pretty cool. You can explore the chat source code and see how easy it is to make. However, lets make our own simple demo. Go into the “express-demo” folder you created earlier and create a new file in there called “app.js”. We will borrow the code from the Express intro page to populate the app.js file you just created but I’ll modify it slightly to get the lib path correct.

require.paths.unshift('../express/lib');
require('express');

get('/', function(){
	this.redirect('helloworld')
});

get('/helloworld', function(){
	return 'Hello World';
});

run();

Run it just like you did with the chat program:

node app.js

http://localhost:3000/

You should see something similar to this:

If you want to go further with Express then check out these links:
1. The Express-JS forum
2. The Express Wiki
3. Build a blog with Express, Node and MongoDB
4. Massively-Multiplayer HTML 5 Asteroids built using Express and Node, the code for it is here

Post to Twitter

This entry was posted in JavaScript, Node.js, Ubuntu. Bookmark the permalink.

2 Responses to Getting started using Node.js and Express.js to build a simple website

  1. David Herron says:

    Interesting, but your method to installing Express is rather circuitous. Why not just tell people to install npm (see http://npmjs.org) and then type: npm install express

    Reusing the examples however is a good idea and will require modifications of the sort you mention – however rather than using require.paths.unshift they should be relying on a require statement and the use of npm to install Express in a node_modules directory. The current Node.js documentation has very strong warnings against using require.paths.unshift.

  2. Chad Lung says:

    @David,

    This article was written over a year ago and back then NPM was version 0.1, things have matured a lot in a year for NodeJS as well as Express and NPM. The issue with the tech world is anything that is written is quickly obsolete especially with a project as fast moving as these are. Back then I ran into some issues with NPM, those have now cleared up.

    I’ve blogged at least two articles since then on setting up NPM:
    http://www.giantflyingsaucer.com/blog/?p=2284
    http://www.giantflyingsaucer.com/blog/?p=2775

    Chad

Comments are closed.