Latest node.js, npm, express and jade
I’ve been dabbling with these over the last few months and trying to make some time to use them in some new projects, but they’ve been making great strides recently and it has been hard to keep up with all the developments. I tried to update to node.js v0.5 but in the end got confused with the different mixes of versions between older projects. I’d had some problems installing some of the modules with various dependencies. I decided to start again with the latest on all fronts. It has shown me how different it has become. express has a guide here, http://expressjs.com/guide.html but I just want to give you something simple. Using version 5 should be more compatible with the soon to be released v6, as well.
I’ve been working on a C# driver for node, on Windows, using 5.8 exe which is impressive, but in the following install I’m working on a Mac.
The node modules may now be held within the project directories, rather than centrally, which gives more flexible versioning and the projects are growing together to allow a variety of templating and css engines.
I installed the development version of node, to get the latest 5.10 version; found that I needed latest python27 upgrade as well (in for a penny…)
sudo port install nodejs-devel
and npm was installed to be similarly up to date,
curl http://npmjs.org/install.sh | sudo sh
express now gives similar facilities to Rails and will create a working structure of directories and source code files when creating a new project. I next installed express, but globally, which puts the files at /opt/local/lib/node_modules/express
sudo npm install express -g
and did the same for the jade templating engine,
To create a project, just run express,
sudo express ./foo && cd foo
this will create the directory with all basic project files within it; jade is the default templating engine used. You can add a ‘-s stylus’ option to further add a css engine.
create : ./foo
create : ./foo/package.json
create : ./foo/app.js
create : ./foo/public
create : ./foo/routes
create : ./foo/routes/index.js
create : ./foo/views
create : ./foo/views/layout.jade
create : ./foo/views/index.jade
create : ./foo/public/images
create : ./foo/public/stylesheets
create : ./foo/public/stylesheets/style.css
We then need to run npm to bring in all the dependencies,
sudo npm install
The result is a well structured set of files, built-in session and cookie features and a single command to change easily from development to production (or other) configurations. There is an app.js file that node will use to give the basic site.
One module that I always bring in on the first test is nodemon. It monitors the files in your project. If any changes/edits are gone it restarts the node server. Excellent; it saves so much time, thanks Remy.
I also took some help and inspiration from this url, http://shapeshed.com/journal/creating-a-basic-site-with-node-and-express/