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:

1// "awesomod.js"
2var awesomod = function(str) {
3  return str.replace(/okay/, 'awesome');
4}
5 
6var yawp = function() {
7  console.log("Awesome!");
8}
9 
10module.exports = awesomod;
11module.exports.yawp = yawp;
1// "index.js"
2var awesomod = require("./awesomod");
3 
4s = "I'm okay.  How are you?";
5console.log(awesomod(s));
6awesomod.yawp();

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

1npm install somemodule

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.

1nmp install coffee-script -g
2coffee someapp.coffee

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:

1{
2  "name": "The Okay App",
3  "version": "1.0.0",
4  "dependencies": {
5    "express": ">=2.0.0",
6    "redis": "~0.7",
7    "ejs": "~0.7.1"
8  }
9}

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:

1npm install

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:

1var express = require('express');
2var app = express.express.createServer();
3app.get('/', function(request, response) {
4  // __dirname is the current directory
5  response.sendFile(__dirname + "index.html");
6});
7app.listen(8080);

Route Parameters:

1app.get('/users/:name', function(req, response) {
2  var name = req.params.name;
3  // do stuff with the :name we read it...
4});

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:

1// app.js
2//...
3response.render('users.ejs', {name: name, friends.fr});
4//...
1// users.ejs
2<h1>Welcome, <% name %></h2>
3<ul>
4  <% friends.forEach(function(fr){ %>
5  <li><%= fr.name %></li>
6  <% }); %>
7</ul>

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.

Leave a Reply

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