Stumbling through express & using middleware

I took a break from Meteorjs and went back to nodejs to understand what is special about MEAN (Mangodb, Express, Angularjs and Nodejs). While I was reading through MEAN I realized I did not do much with node or express for that matter. More I read about express, I realized how easy it is to get going on it. So I started to read couple of blogs and trying out simple todo project to understand express.

Here are couple of things I ran into so far;

  1. It seems you need to install express, connect and jade in the local project directory. For some reason, even though I installed it with –g option, it was not picking it up. There must be something I am doing wrong here but for now I made it to work with local install.
  2. I spend some time figuring this out, make sure the routes are properly defined. I had the following

app.get(‘/’, ‘home.index’)

as you can guess, ‘home.index’ is routes to JavaScript code which shouldn’t be in quotes.

3. This is important, if you are trying to follow some blogs, you will run into problems as express evolved and some of the blogs are outdated (so will this). But as of 0.10 version, middleware portions of the express were removed from it and it is now available under connect. So if you want to use middle ware, in my case, I want to read and parse the body part, I need body parser which is now part of connect npm package. So to access something like the following in my controllers


I need to do the following

  • npm install connect
  • in the require, add

middleware = require(‘connect’)

next you need to add body parser to the express to use


One side note from ‘Good parts of Javascript’: ‘Make a habit to have curly braces on the right of the statement instead of the next line’. Fortunatly coming from C#, I always practiced that so I am in good shape Smile