Tuesday, July 5, 2011

Setting up node.js express to work as an MVC framework - Basic tutorial

Recently, I gave node.js a try after hearing all the hype about its potential and, honestly, it gave me the perfect excuse to breakout and dust off my Linux skills. To make a long story short, I ended up doing a fresh installation of Ubuntu 11.04 and deleted my aging Ubuntu OS. This was not necessary and not suggested.

After getting everything to work, I finally wanted to learn how it worked. Therefore, I read The Node Beginner Book by Manual Kiessling. An excellent introductory tutorial that should not be skipped. After become an advanced novice, I wanted to dive into developing my own rapid prototypes. However, I am used to working with CakePHP and wanted a similar style framework to make my mock ups quick. The following is what I learned from my endeavors.

Node.js as MVC

Install the express node module
npm install -g express

Create the project folder
express /path/to/project

Install the express folder in the folder of your choice. I chose to put my in the same folder that node.js belongs in
cd /path/to/folder node is in/ && npm install express

As my Linux skills still have room for improvement, I used the gui and went to express/examples/mvc and copied all and went to /path/to/project and pasted all overriding all the old stuff.

Open app.js with your favorite editor and change line 6 to
var express = require('express')

Open mvc.js and change line 7 to
express = require('express')

Go back to the terminal and install ejs
npm install -g ejs

Go to the project folder and enter node_modules
cd /path/to/project/node_modules

Install the source code of ejs
npm install ejs

Now that all modules are loaded go back to the project folder and update the dependencies
cd .. && npm install -d

The MVC framework should be ready to go. I'll be posting examples using the MVC framework shortly. So keep watching.