Getting started with Express for node.js

24.05.2012 17:25Comments
Express is an MVC framework built on top of connect that obviously runs on node.js. Express simplifies making websites by adding a series of middleware that will handle the parsing of the requests, the routing, and finally the rendering of the views. I’ll show you how to do your first express.js app which will be extremely easy and simple to do. First, create a folder for the app, and install the Express module with npm like this: c:\node\testapp>npm intall express installexpress This will install the express module, and will leave a command line tool that will let us create a simple express app. For executing this command line type the following: c:\node\testapp>.\node_modules\.bin\express expresscmd As we can see, express created three folders for us: Public: these are static files such as .html, .js, .css and images that are considered public and will returned directly if those are requested. Routes: here is where our controllers will reside. In this folder we will put the scripts that know how to handle requests, and which views to render or which response to send back. Views: obviously the views are going to be in this folder. Express can handle quite a lot of templating engines which you can choose from. By default Express uses jade (.jade files) which is a very clean and simple syntax. The other thing that the express command is letting us know, is that we have to run npm tool to update the dependencies. So if we open the package.json we can see that our application right now depends on both Express and Jade: { "name": "application-name" , "version": "0.0.1" , "private": true , "dependencies": { "express": "2.5.8" , "jade": ">= 0.0.1" } } So let’s run npm to install the necessary dependencies for our app: c:\node\testapp>npm install npminstall Let’s run the app so see it in action, and we’ll go through each piece later. Use the following to run the website: c:\node\testapp>node app.js nodeapp Now go to http://localhost:3000 in your browser and you’ll see a site with a “Welcome to Express” message. Now let’s see how all this is happening. Express is built on top of connect.js which allows you to plug middleware in the pipe to handle requests. When you look at the app.js file, you’ll notice two things, one is that it’s using the app.router middleware: approuter This middleware will allow you to define routes, and specify the functions that will handle that request. The other thing you’ll notice is that after the app is configured, there’s a Routes comment with the following: routes Which is saying that it will handle requests to “/” with routes.index. The routes as I pointed out before, are stored in the “routes” folder, so if we look at the routes\index.js file we’ll see how that request is being handled. routeindex Express will figure out which templating engine we are using (Jade in this case), will map the name of the view “index” to the right file, and send back the result of applying that view to the model we passed; which in this case is { title: ‘Express’ }. In our case, the index view is stored in views\index.jade which is using views\layout.jade as the layout (or master page for the ones coming from asp .net). Here’s what it looks like: indexjade Pretty cool, right?? So that’s it for getting started on using Express.

comments powered by Disqus