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:

// "awesomod.js"
var awesomod = function(str) {
  return str.replace(/okay/, 'awesome');
}

var yawp = function() { 
  console.log("Awesome!");
} 

module.exports = awesomod;
module.exports.yawp = yawp;
// "index.js"
var awesomod = require("./awesomod");

s = "I'm okay.  How are you?";
console.log(awesomod(s));
awesomod.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

npm 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.

nmp install coffee-script -g
coffee 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:

{
  "name": "The Okay App",
  "version": "1.0.0",
  "dependencies": {
    "express": ">=2.0.0",
    "redis": "~0.7",
    "ejs": "~0.7.1"
  }
}

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:

npm 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:

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

Route Parameters:

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

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:

// app.js
//...
response.render('users.ejs', {name: name, friends.fr});
//...
// users.ejs
<h1>Welcome, <% name %></h2>
<ul>
	<% friends.forEach(function(fr){ %>
	<li><%= fr.name %></li>
	<% }); %>
</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 *