Jul 12
2012

Node.js notes: Code School’s levels 4-5

This is a collection of my notes on Code School’s, Realtime Web with Node.js course. I posted notes on levels 1-3, yesterday. They covered setting up a node server, using curl, read streams and write streams.

Level 4

You can import external modules by using require('somemodule');. Exporting is done by setting the variable exports to some function. You can also set properties of exports to functions. Here’s an example that shows both:

Since we exported the function awesomod directly, that is what is called on line 5. It replaces the word “okay” with “awesome”. The yawp function was attached onto the module with module.exports.yawp = yawp;, so it’s found at awesomod.yawp

Using NPM

The Node Package Manager is great! It works just like apt-get or ruby gem. It goes out onto the internet and fetches the stuff you want to install. The basic syntax is

Afterwards you can access that package inside your javascripts with require('somemodule');, as shown above. Note that when there’s no ./ in front of the module name, node will look for that module inside the default directory where NPM installs them.

To install programs you can run from the command line, such as coffee script, for example, use global -g flag.

Setting up dependencies

By stating what dependencies your node app needs, you can use a single NPM command and it will go out and get all the modules on your dependencies list and the dependencies for those modules. Dependencies are specified by adding an object to a file package.json in your apps root directory. Here’s an example of a basic package.json:

There are a lot of options for setting up your dependencies.
= exactly that version
> higher than that version (Dangerous since the API could change!)
>= same or higher than that version (Dangerous!)
~0.7 at least 0.7 but less than 1.0 (Dangerous!)
~0.7.1 at least 0.7.1 but less than 0.8.0

After this package.json file is in place, just run one command to get them all:

Level 5

Level 5 was all about using Express.js, and for me it was the best part of the entire course. Node.js is very low level, which is great for making file uploading tools or simple chat programs or backend pieces of huge systems that need to run asynchronously… but it’s a ton of work to do stuff like say making a database-powered website. The 15 minute “build a blog” demo that made such a big splash for rails would be a pain to do in Node.js directly. Express solves a lot of this pain. It’s built on top of Node.js and provides help with routing, a template system for making views and a few other things. It certainly doesn’t do all the things rails does for you, but it does a lot. From what I understand, it’s more similar to the Sinatra framework.

Start the server and serve index.html:

Route Parameters:

Templates

Express Templates are .ejs files and they work just like the .erb files in rails — Most of it is HTML, but the stuff between <% tags %> is executed as javascript and javascript between <%= %> tags gets printed on the screen. The blocks between these tags has access to parameters passed in from the server. In this example an object with names and friends is passed to the template:

This render a page for each users/1 that greets user#1 by name and has user#1’s friends listed in an unordered list with one item generated with a name on it for each friend. At users/2 a page will be rendered that greets user#2 and has user#2’s friends and so on for every user the app has stored.

This entry was tagged , , , .
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *