Skip to content

node.js first try with express and jade

December 28, 2011

In order to get the currently most-praised high performance server side technology, we started to experiment with node.js.

The very first look at the project page reveals a whole ecosystem of libraries and possibilities. So the first task is to fix the concrete setup for the test. express and jade seem to be quite a commonly used combination. So let’s start!

We follow this excellent tutorial: which, for installation, immediately redirects here: We install from source since we’ll need to do this on the test machine also:

$ curl > node-v0.6.6.tar.gz
$ tar zxf node-v0.6.6.tar.gz
$ cd node-v0.6.6
$ ./configure --prefix ../node
$ make
$ make install
$ PATH=$PATH:/Full/Path/to/node/bin
This lasts some time but ultimately node.js is up and running. Now install express:
$ npm install -g express
$ express nodejs
$ cd nodejs
$ npm install
 Watch how Jade is installed in this last step also. Now, time to start:
$ node app.js
Express server listening on port 3000 in development mode
 Point your browser to http://localhost:3000 and look at you first page. Where does it come from? We clarify that by adding our first page, the search form. First, we need to route “GET /search” to a javascript function rendering some template. Therefore, open app.js and find the statement app.listen(3000), which starts the previously configured server on port 3000. Right before, the route for the index page is added. So this is a good place to add our new route:
 This statement registers method search of the routes modules to the URI /search. The method itself has to be added in the file routes/index.js, where you already find a function for the index page. So add: = function(req, res){
  console.log("search invoked");
  res.render('search', {title: 'Search'})
Here the request is delegated to some template engine (jade in our case, because this is pre-configured in app.js). So create the file views/search.jade and enter:
h1 Search Hotels
 form(id="searchCriteria", action="hotels", method="get")
    label(for="searchString") Search String:
    label(for="pageSize") Maximum results:
     option(value="5") 5
     option(value="10") 10
     option(value="20") 20
     button(type="submit") Find Hotels

Now restart node and point your browser to http://localhost:3000/search and watch your first page! It is now a tedious, but simple work to convert the other pages to jade templates and add appropriate routes. Wait – we should take a look at database access first!

From → node.js

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: