Node Roundup: 0.6.10, 0.7.3, Backbone.IO, Notes

08 Feb 2012 | By Alex Young | Comments | Tags node modules fibers backbone.js
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

0.6.10, 0.7.3

Node 0.6.10 was released last week. Of interest to Windows users is the following:

  • Add npm msysgit bash shim to msi installer
  • dgram: Implement udp multicast methods on Windows

Node 0.7.3 has also been released. This reverts support for isolates:

It was decided that the performance benefits that isolates offer (faster spin-up times for worker processes, faster inter-worker communication, possibly a lower memory footprint) are not actual bottlenecks for most people and do not outweigh the potential stability issues and intrusive changes to the code base that first-class support for isolates requires.

Ben Noordhuis finishes the commit message with “Good bye, isolates. We hardly knew ye”. I couldn’t find any discussions about this in the nodejs-dev group, but I noticed David Herron mention it here: Good bye isolates, Node.js hardly knew ye.


Backbone.IO (License: MIT, npm: by Scott Nelson is a Backbone.js module that can synchronise multiple clients. When a model is synced, the server-side code will trigger events on collections across multiple clients.

The server-side code uses a Connect-inspired middleware API. Notice the familiar signature:

var backend = backboneio.createBackend();

backend.use(function(req, res, next) {


Scott has included tests as well, which are built using Mocha and Sinon.


Notes by Oleg Podsechin is an example app built using his Common Node and Mongo Sync libraries. This is an effort to bring traditional declarative synchronous code to Node through node-fibers.

Here’s a taster:

// Get an array of notes

// Save a note
mongo.db('notes').getCollection('notes').save({ name: });

jQuery Roundup: 1.7.2, Super Labels, jquery.textntags

07 Feb 2012 | By Alex Young | Comments | Tags jquery plugins forms
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

jQuery 1.7.2

jQuery 1.7.2 Beta 1 has been released. There are a lot of bug fixes, and some interesting API tweaks:

  • #5571: Allow chaining when passing undefined to any setter in jQuery
  • #8498: Animate hooks
  • #11119: The curCSS function needs only 2 arguments
  • #10931: Unit tests shouldn’t require Internet access

That last one is particularly useful if you need to run jQuery’s unit tests. Tests shouldn’t need a connection!

jQuery Super Labels

jQuery Super Labels (License: MIT) by Rémy Bach is a form field overlay plugin that hides labels when an input gains focus. It’ll work automatically with most text fields simply by calling $('form').superLabels(). If the label needs to be displayed in a different location, suitable options can be provided with labelLeft and labelTop.

Super Labels has advanced options for controlling animation easing and duration.

Another interesting plugin by the same author is jQuery Slash Search which will focus on a search field when / is typed.


textntags screenshot

jquery.textntags (License: MIT, GitHub: daniel-zahariev / jquery-textntags) by Daniel Zahariev is another @name input enhancement tool. A full example of it using Ajax to search looks like this:

  onDataRequest: function(mode, query, triggerChar, callback) {
    $.getJSON('assets/data.json', function(responseData) {
      query = query.toLowerCase();
      responseData = _.filter(responseData, function(item) { return > -1; });, responseData);

This is an early version of the library, so the author doesn’t make any promises in terms of browser support. However, the documentation is solid and it ships with CSS, so it’s easy to drop into a project.

UIKit, Expect-dom, Patio

06 Feb 2012 | By Alex Young | Comments | Tags node testing jquery


UIKit banner

TJ Holowaychuk’s latest project is UIKit (License: MIT, GitHub: visionmedia / uikit, npm: uikit). It’s a small library of decoupled components for creating web interfaces. Simple, structural markup and modular JavaScript is used to create widgets like a “card” that uses 3D transforms, dialogs, notification messages, and more.

UIKit reminded me of Nib, TJ’s library of Stylus mixins, utilities, and components. TJ stresses that UIKit isn’t a CSS framework like Bootstrap, but neither is it written with Stylus and Jade. Instead it functions as a loosely-knit set of components that can be built on.


Expect-dom (License: MIT) by Kevin Dente is a set of DOM-related assertions for expect.js. Various assertions can be made, including attr, id, and html:

expect($('<div title="some title"></div>')).to.have.attr("title");

The author has adapted code from jasmine-jquery to create this library.


Patio (License: MIT, GitHub: Pollenware / patio, npm: patio) from Pollenware is an SQL library inspired by Sequel. It supports schema creation, migrations, queries, models, and associations. It even includes handy flow control related methods like then, removing the need to heavily nest certain asynchronous operations:

User.findById(1).then(function(user) {
  // SELECT * FROM user WHERE id = 1 

Patio also has some handy JavaScript-friendly utility methods:

User.toHash('id', 'name').then(function(nameIdMap) {
  // SELECT * FROM user 
  //{"1":"Bob Yukon"}

Full API documentation is also available: Patio API documentation.

The project still only has 11 GitHub followers, but it’s got an incredible amount of functionality already – check it out and give the authors some feedback!

StackHack, Ducks, Remote-Tilt, Simplify.js

03 Feb 2012 | By Alex Young | Comments | Tags node webgl testing games mobile


StackHack screenshot

StackHack by Philip Deschaine and from PubNub is a WebGL demo that uses PubNub to create a massively multiplayer interactive block stacking game. The technical details are discussed in StackHack: A Massively-Multiplayer Mashup of PubNub and Three.js.

Let’s start with the server. I used Node.js with express to serve up our HTML, CSS, JavaScript. When a client connects, we generate a UUID, append some stuff and listen on that channel. Why do it this way? Why not just use a generic PubNub channel? Excellent question I wanted what’s known as an authoritative server.

The article includes more details behind both the client-side and server-side code.

Ducks, a WebGL Demo

Ducks screenshot

Ducks (GitHub: statico / webgl-demos / ducks) by Ian Langworth is a simple game demo that uses WebGL and sound, complete with animated models and reflections. The GLGE WebGL framework has been used, along with models from the COLLADA Basic Samples collection.

The game logic, in ducks / logic.js, is relatively clear and easy to follow, so it works as a great example of a small, manageable example of a WebGL game.


Remote-Tilt (License: MIT, GitHub: remy / remote-tilt) by Remy Sharp can help test motion events without fooling around with a mobile device:

Testing motion events was never going to be easy task. You have two options, both of which suck. That’s where Remote-Tilt comes in. By including a single line of JavaScript you can emulate device motion events in your test page which can either be a regular browser or even a mobile emulator.

By including the Remote-Tilt polyfill on a page a popup will appear that allows motion events to be simulated:

Remote-Tilt motion emulator


Simplify.js demo screenshot

Simplify.js (License: BSD, GitHub: mourner / simplify-js, npm: simplify-js) by Vladimir Agafonkin is a library for fast 2D/3D polyline simplification:

It is very useful when you deal with lines consisting of many tens of thousands of points, e.g. when you need to quickly render a 50k-points line on the browser (for charts, map routes, etc.).

The demo on the Simplify.js homepage shows how impressive the performance is, easily throwing around 50,000 points like it’s child’s play! The source for this project has been extracted from another of Vladimir’s interesting libraries, Leaflet, which is also worth checking out.

Mastering Console Logging

02 Feb 2012 | By Alex Young | Comments | Tags node modules tutorials

The console object crops up everywhere. But what is it, and what can it do? Most people seem to use it without realising the sheer convenience it can provide. Let’s take a look at where it comes from, and how to use it correctly.

Built-in vs. Host

The console object is what’s known as a host object in ECMAScript. Host objects are supplied by the host environment. Node’s documentation refers to console as a “global object”.

This is different to a native object which is an object in the ECMAScript implementation in question, and defined by the specification rather than the host environment. Built-in objects are similar, but are present when an ECMAScript program starts. In addition, built-in objects inherit from Object or Function whereas host objects might not.

Host objects may also not be available when execution starts. That’s why some browsers treat console differently depending on whether the output is visible or not. Notice that Mozilla’s documentation states that this behaviour has changed:

Prior to Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0) , the console object’s methods only work when the Web Console is open. Starting with Gecko 12.0, output is cached until the Web Console is opened, then displayed at that time.


Since console isn’t yet formally covered by a specification, implementations vary. In Node it’s closely tied to standard input output, referred to as “stdio” by the documentation. This gives rise to error and log methods that will be printed to the appropriate output stream.

Most implementations give us convenient ways of separating output for debugging messages, errors, and warnings:

console.log('This is merely an informational debug message');
console.warn('This is a warning');
console.error('This is an error');

These messages will be handled differently depending on the environment. Firefox 10’s built-in developer console will display an “X” next to the error, whereas Node will print to stderr.


Console redirection example

When writing Node scripts, it’s a good idea to use console.error when displaying errors. It means that errors can be redirected in the shell – sometimes I don’t want to see standard output, but I do care about errors, so I’ll redirect the errors into a log file.

I/O redirection generally works like this: 1> will redirect to stdout, and 2> redirects stderror. Given this example:

console.log('This is just noisy debug stuff');
console.error('This means Alex broke something again');

Then running node test.js 1> debug.log 2> errors.log will redirect the debug and error messages to separate files. Using 1> will truncate the file to a length of zero, so to append messages 1>> and 2>> can be used instead.

 Inspection and Concatenation

In most browsers and Node, objects will be automatically printed in a readable format when using console.log. That means printing variables is possible without any extra effort:

console.log({ alex: "some dude" });

In Node, util.format is applied to the arguments, which runs util.inspect. Therefore, there’s no need to run console.log(util.inspect(value)) in Node!

Most implementations will automatically concatenate values:

var name = 'Alex';
console.log('Name:', name);
// Name: Alex

Notice how a space is automatically added? There’s no need to manually concatenate a value using console.log('Message: ' + value). In fact, doing this will mean the value won’t be automatically inspected.

Node’s format method uses Array.prototype.join to do this: lib/util.js.


Recent implementations also provide nascent string substitution support:

console.log('Name: %s, Intergalactic Credits: %d.', 'Alex', -100).

Support for substitution strings isn’t exactly printf yet, and with supporting between environments varying somewhat.


Both Node and Mozilla provide console.dir: Node runs util.inspect on the supplied argument and prints the output to stderr. Conversely, Firefox will display an interactive version of the object, with disclosure triangles if necessary.

Need to quickly benchmark a slow operation? The console.time and console.timeEnd methods can also be used in both environments. The time method accepts a parameter which allows the timer to be named:

for (var i = 0; i < 100; i++) {

Stack traces can be displayed with console.trace(). Again, in Firefox this will be interactive, and in Node it’ll be redirected to stderr.


The console object provides a surprisingly useful amount of functionality. If you’re writing lightweight Node programs, or want to debug something in a relatively modern browser (or browser with suitable developer tools), then try to take advantage of it rather than relying on unnecessary libraries.

In general, when logging with console:

  • Use the correct logging method so redirection works as expected
  • Quickly append variables to messages using console.log('Message:', value)
  • Use console to automatically inspect variables


Node Roundup: Ryan Dahl Steps Down, Thimble, Mongo Model, Banking.js, Navcodec

01 Feb 2012 | By Alex Young | Comments | Tags node modules banking frameworks mongo
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

Ryan Dahl Steps Down

Ryan Dahl announced that he’s stepping down as “gatekeeper” and Isaac Schlueter will now take the lead. Many messages of gratitude were posted to the nodejs group in response.

Our energy will now be largely focused over the next few months on improving the third party module system experience including a website for browsing modules, a new addon build system, and binary installations from npm.

The thing that’s fascinated me most about Ryan and Node over the last three years is the creation and development of libuv. Ryan made a great screencast about libuv, and something about his attitude came across as relatively humble and reserved, which impressed me given the success of Node.

If you’ve followed Isaac’s blog you’ll know he has a different personality – I think it’s fair to say he’s got some strong opinions about software development, but argues them in a well-tempered manner. Time will tell how these different personalities will impact on Node’s overall development and community.

Meanwhile, Node 0.6.9 was released which brings back some missing dgram functionality, and includes bug fixes.


Thimble (License: MIT, GitHub: MatthewMueller / thimble, npm: thimble) by Matthew Mueller is a new way of working with Express. It inserts two middleware layers that manipulates requests and assets to provide a more integrated way of working with client-side code. Thimble’s functionality is provided through plugins, which include:

  • Flatten: Introduces the include tag to HTML templates
  • Embed: Allows script tags to compile templates into functions
  • Package: Compiles an application

Matthew has created two examples of Thimble projects, available here: thimble / examples.

Mongo Model

Mongo Model (GitHub: alexeypetrushin / mongo-model, License: MIT, npm: mongo-model) by Alexey Petrushin is a new MongoDB library that can optionally work with fibers.

I had trouble running the library without CoffeeScript, so I installed it and ran the examples with coffee and they seemed to work. The mongo-model documentation is all CoffeeScript as well, so you may struggle to use it if you want to use it with JavaScript.


Banking.js (License: MIT, GitHub: euforic / banking.js, npm: banking) by Christian Sullivan is a unified API for North American banks. Rather than logging into a clunky web interface banking.getStatement can be used to get statement details.

Imagine scripting some Arduino hardware with a suitable Node Arduino package to disable your current debt in an amusing way!

Node libavcodec bindings

These Node libavcodec bindings (License: MIT, npm: navcodec) developed by Optimal Bits offer a new way to work with libavcodec in Node. The authors have aimed to create a new JavaScript API that works how JavaScript developers would expect:

navcodec = require('navcodec');'', function(err, media) {
  if (media){
    media.addOutput('myoutput.mp4', {
      width: 640,
      height: 480,
      audioBitrate: 128000,
      videoBitrate: 500000

    media.transcode(function(err, progress, time) {
      if (progress === 100){
        console.log('Total transcoding time:' + time);

This seems like an incredibly useful library for those that work with media. In particular, metadata extraction and thumbnail generation all become possible with navcodec.

jQuery Roundup: jPages, youRhere, jquery.lazyLoader, Deferred and Promise in jQuery

31 Jan 2012 | By Alex Young | Comments | Tags jquery plugins lazyloading pagination promises
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.


jPages (License: MIT, GitHub: luis-almeida / jPages) by Luis Almeida is a client-side pagination plugin that can page through a set of elements in an unordered list. Given some suitable HTML, perhaps containing a list of images:

<!-- Future navigation panel -->
<div class="holder"></div>

<!-- Item container (doesn't need to be an UL) -->
<ul id="itemContainer">
    <!-- Items -->

Then to get pagination controls the only JavaScript we need to use is $('div.holder').jPages({ containerID : 'itemContainer' });.

The author has written lots of demos, applying the plugin to lazy loading images, or even using titles as links to create sub-navigation for a page.


You Are Here

youRhere (License: MIT or GPL, GitHub: fastrd / youRhere) by Daniel Sternlicht transforms pages of text to allow the reader’s progress to be saved. The current mouse position is displayed with an arrow, and clicking on a line will highlight it. This can persist by using localStorage.

Basic usage is just $('#content').yourhere();.


jquery.lazyLoader (License: MIT, GitHub: davetayls / jquery.lazyLoader by Dave Taylor aims to make image loading more responsive by loading images based on the browser viewport size. With carefully named images it can work pretty much automatically:

  img: function(url, windowWidth) {
    if (windowWidth >= 768){
      return url.replace(/.(jpg|gif|png)$/i, '-mega.$1'); 
    } else {
      return url;

jQuery plugin authors take note! Dave has raised the bar! He’s not only included tests, but he’s also running them through a CI server (davetayls/jquery.lazyLoader)! I’m not saying he’s the first person to do this, but given the amount of plugins we receive at DailyJS with no tests I found it a pleasant surprise.

Deferred and Promise in jQuery

Deferred and promise in jQuery by Edwin Martin is an introduction to the tools jQuery provides for working with these functional concepts.

So what is a deferred and what is the difference with a promise? As you have seen above, a promise is an object that is returned from an asynchronous function. You need a deferred when you write such a function yourself.

Edwin aims to explain how to use promises and $.Deferred, and also explain how they’re different.

HelsinkiJS February, Todo, Testing Backbone.js

30 Jan 2012 | By Alex Young | Comments | Tags libraries testing node browser events backbone.js

HelsinkiJS February

HelsinkiJS February has been announced, and will take place on the 16th of February at Codento. Two speakers are attending so far: Jarno Keskikangas and @polarblau.

If you want me to include your JavaScript event on DailyJS, just @dailyjs or use our contact form to get in touch!


Todo screenshot

Todo (License: MIT, npm: todo) by Veselin Todorov is a small todo list application for the command-line interface, written with Node. I like seeing clever Unix utilities and command-line applications made with Node, and this one seems cool with its coloured UI and UTF-8 characters.

The interesting thing about this tool is it’s built using Flatiron. Flatiron is designed to be adaptable, and although it’s aimed at web development, anything can (and will) be built with it.

If you’re a fellow command-line hacker and have mastered pretty colours and option parsing, then another useful thing is to include a man page. This is supported by npm: check out npm help json and scroll down to the “man” section.

Testing Backbone.js Best Practices

Testing Backbone.js Best Practices by Jonathan Eatherly includes some useful tips. I’ve often felt like testing Backbone.js is a little bit hard to visualise, but Jonathan makes a few confusing aspects much clearer:

By default a Backbone view will create an in-memory jQuery object containing a single DIV element … This means we don’t have to see the ugly injecting and removing of content on the page as the tests run, there is no need to clean up the DOM after each test iteration, and our test suite will run much faster.

His examples use the Jasmine test framework, but much of this advice can be applied to any test framework.

Resumable.js, Open Source Android WebGL Implementation, Coderbyte

27 Jan 2012 | By Alex Young | Comments | Tags node modules webgl html5


Resumable.js (GitHub: 23 / resumable.js, License: MIT) from 23 is a library for managing concurrent and resumable uploads using the HTML5 File API:

The library is designed to introduce fault-tolerance into the upload of large files through HTTP. This is done by splitting each files into small chunks; whenever the upload of a chunk fails, uploading is retried until the procedure completes. This allows uploads to automatically resume uploading after a network connection is lost either locally or to the server.

Since the File API has limited support, the author’s example should degrade:

var r = new Resumable({
  target: '/api/photo/redeem-upload-token',
  query: { upload_token:'my_token' }
// Resumable.js isn't supported, fall back on a different method
if (! location.href = '/old-uploader';

Resumable.js’s API is event based, and events include fileAdded, fileSuccess, and fileError.

To implement the server-side part of this, the state of uploaded chunks should be managed. The documentation notes that on unstable networks the same chunk may be uploaded more than once, so appropriate HTTP status codes should be returned as chunks are received.

 Open Source Android WebGL Implementation

Sony Ericsson announced WebGL support for Android 4.0 and published the source on GitHub at sonyericssondev / WebGL:

It is based on ics-mr1, and requires that webkit is built with USE_ACCELERATED_COMPOSITING and that the browser application is built with hardwareAccelerated=true.

This is the code that will be used on forthcoming Xperia phones running Ice Cream Sandwich.


Coderbyte is a service designed to “practice and perfect” programming skills. At the moment it includes challenges for client-side JavaScript, and there are some guest challenges that you can try out here: Coderbyte Coding Area.

With the right community tools I think Coderbyte could be a little bit like Stack Overflow in reverse.

Using Node Modules Effectively

26 Jan 2012 | By Alex Young | Comments | Tags node modules tutorials

Node’s module system is deceptively clever. On the surface it seems like it provides just enough functionality to get by, but it can go a long way towards helping to structure an application elegantly. Node’s official documentation covers all of the major features, yet many modules sent in for review at DailyJS don’t use these techniques where they could.

For this article I’ll be using Express applications as an example, but these techniques can be applied to any Node application, whether it’s a command-line tool or TCP service.

Splitting up Large Files

Most Express tutorials and examples have a monolithic file that does everything, but Node’s module system gives us some tools to split the file up without much effort. Express routes are a good target for reorganisation.

Given a set of routes:

app.get('/', function() {});

app.get('/users', function() {});
app.get('/users/:id', function() {});

Then we can reorganise them by creating a folder called routes/, and creating files with the following structure:

├── index.js
├── main.js
└── users.js

Express developers often use main.js as the convention for things that aren’t easily split into RESTful groups. In this example I’d put the '/' route in there. The extra file, index.js would look like this:


Now our app.js file that loads Express and instantiates an app object can have all of the routes removed and replaced with require('./routes');.

Two things make this example work: Node will automatically look inside a folder for index.js, and app is a global.

In Node, defining app using var will restrict its scope to the current module. By purposefully not defining app with var it’s accessible inside the routes files. Otherwise we’d have to pass it to each routes module somehow. I see modules structured like this a lot:

module.exports = function(app) {
  app.get('/thing', function() {});

Speaking of coupling, using globals can quickly lead to tightly coupled code that makes reuse harder. Making app global in an Express application can help cut down a little bit of boilerplate. I generally keep my routes lightweight so the core functionality is less coupled to Express. In addition, I’ll often create new modules for functionality that seems generic, so I can reuse it in other Express apps.

Configurable Folders as Modules

Node can also be told where to look when loading modules by using a package.json file. This allows the module’s main file to have a different name:

{ "name" : "sub-module"
, "main" : "./path/main.js" }

This seems to be used a lot less than the index.js convention, but it may help you fit it to your application’s internal naming scheme.

Tracking require

Node sets require.main to its module when run directly, but doesn’t when loaded using require. I’ve also used module.parent to achieve this in the past.

These values are often used to determine if an Express application should call app.listen() or not, because not all tests want the same behaviour so conditionally listening based on environment isn’t always the best approach.

For example:

if (!module.parent) {

Private Modules

Using package.json to manage dependencies and other metadata is great, even for private, closed source projects. However, there’s always the nagging feeling that someone could accidentally publish a package when working on commercially sensitive projects. Fortunately, setting "private": true will prevent npm from publishing the package.


If you’re primarily a client-side developer and have been writing libraries using AMD, using it in Node may appeal to you. However, the nodejs-dev group has had several “mile-high threads” on the subject, and support has moved to userland.

RequireJS provides a Node implementation for AMD. The amdefine module implements define.

I’d be tempted to load define as a global in a main module using define = (require('amdefine'))(module);. This allows modules to be structured with a leading define( call, so they could be shared between the browser and server without any boilerplate.

Node Roundup: 0.6.8 and 0.7.1, Summit Coverage, Connect Router, Mongolian DeadBeef, AWS

25 Jan 2012 | By Alex Young | Comments | Tags node modules express middleware mongo amazon
You can send in your Node projects for review through our contact form or @dailyjs.

Node 0.6.8 and 0.7.1

Node 0.6.8 is out. V8 and npm are both updated in this release, and there are also numerous bug fixes including one for the cluster module and another for fs.stat in Windows.

Meanwhile, the unstable branch has been updated to Node 0.7.1. I noticed that this brings V8 up to 3.8.8 which is the latest version in the V8 Changelog.

Node Summit Coverage

This week is the Node Summit, and David Herron has been writing up some detailed coverage at This includes talks from Microsoft, Yahoo, VMWare, and Heroku, so it’s interesting stuff for anyone interested in Node, the cloud, and the near future of Node development.

Node is a great fit for some applications, a terrible fit for others. There’s a danger in the Node excitement to try and use Node to solve every problem. Hence, PAAS needs to be polyglot.

From Node.js Summit: Platform as a Service.

 Connect Router

Connect Router syntax diagram

Connect Router (License: MIT, npm: connect-router) by Craig Condon is a unique take on routing in Express by using strings to express relationships between routes, HTTP methods, and middleware. Routes can be loaded from a file or directory, making splitting up applications into logical groupings extremely easy.

The basic API looks like this:

app.use(connectRouter(function(router) {
  router.on('parseBody', express.bodyParser());

  router.on('user/exists', function(req, res, next) {
    if (userExists(req.query.username)) {
      res.send('That username already exists');

  router.on('-method=POST parseBody -> user/exists -> signup', function(req, res, next) {
    res.send('Successfuly signed up ');

Notice that existing Express middleware can be applied to a router – in this case bodyParser has been used.

Rather than using an API based around HTTP verbs, Craig’s routers look like EventEmitter objects, and use a DSL to define what HTTP verbs should be applied. Middleware can be triggered by listing it like this: 'user/exists -> signup'. Middleware can also be greedy, which is a convenient way of applying permissions to sets of routes.

Mongolian DeadBeef

Have you always wanted a Node MongoDB driver that has the same API as MongoDB’s shell? It seems so obvious, yet it’s tantalizingly hard to find such a module. Oleg Podsechin said he’s been using Mongolian DeadBeef (License: zlib, npm: mongolian) by Marcello Bastéa-Forte and it looks like it does the job admirably. It even includes GridFS support using streams.

Collections can be accessed using db.collection(), and then records can be found and inserted with familiar methods like collection.insert(), collection.findOne(), and collection.find. It allows queries to be built up using chains, like this: collection.find().limit(5).sort({ created: 1 }).

Amazon Web Services Node Library

It seems like Amazon are hell-bent on creating a service for everything in the universe. How can a Node hacker take advantage of this? Well, aws-lib (License: MIT, npm: aws-lib) by Mirko Kiefer is an Amazon Web Services library that provides clients for EC2, Product Advertising API, SimpleDB, SQS, SNS, SES, and ELB.

The project is dubbed as “simple” but already seems fairly extensive. Thanks to some recent Hacker News coverage it currently has 260 followers on GitHub.

jQuery Roundup: Publish Subscribe, Transparency, slabText

24 Jan 2012 | By Alex Young | Comments | Tags jquery plugins graphics text events templating
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

Publish Subscribe Plugin

Joseph Zimmerman’s Publish Subscribe Plugin (License: GPL) is an implementation of the aforementioned messaging pattern aimed at browser-based JavaScript:

var handle = $.subscribe('foo', function(topic, data) {
  console.log(data, topic);

$.publish('foo bar', 'This is some data');


The author has implemented it with core jQuery methods like $.type and $.each so the source is readily understandable.


Transparency (GitHub: leonidas / transparency, License: MIT) by Jarno Keskikangas is a template engine for jQuery that maps JSON to DOM elements using a $.render method:

var hello = {
  hello:   'Hello',
  goodbye: 'Goodbye!'


This example would write the values to tags matching the selectors .hello and .goodbye:

Transparency relies on convention over configuration and requires you to have 1:1 match between CSS classes and JSON objects. The idea is to minimize the cognitive noise you have to deal with. Just call $('.container').render(data) and move on.

There’s a detailed blog post about Transparency here: Implementing Semantic Anti-Templating With jQuery.


slabText example

slabText (GitHub: freqdec / slabText, License: MIT/GPLv2) by Brian McAllister splits headlines into rows, and resizes them to fill the available space. This works as the browser viewport changes.

Brian notes that this is based on Erik Loyer’s slabtype algorithm, which is interesting reading for those inspired by FitText.

Gbone.js, Writing Browser Extensions, Font.js

23 Jan 2012 | By Alex Young | Comments | Tags backbone.js extensions plugins fonts


Gbone.js (License: MIT) by Gobhi Theivendran is a framework built on top of Backbone.js for building mobile applications, inspired by Spine Mobile. It contains several classes that extend Backbone’s classes to make it easier to build mobile applications. For example, Gbone.Stage contains multiple Gbone.Panels, and the panels are managed internally by a panel manager. They have a default skeleton like this:

<div class="container">

Panels can be activated and deactivated, animated using a transition. Only one stage and panel can be active at one time, so the end result is similar to native Android and iOS interfaces.

There’s a Gbone.js demo app, which is a rewrite of Gbone.js works with both jQuery and Zepto.

Writing Browser Extensions - Comparing Firefox, Chrome and Opera

Writing Browser Extensions - Comparing Firefox, Chrome and Opera by Parashuram Narasimhan explores writing a browser extension for multiple browsers. The author covers a lot of topics, including manifest files, background processes, message passing, and debugging.

This tutorial is based on Parashuram’s experiences writing the MediaPlus, so some of the coverage is specific to that, but it’s general enough to get a good overview of how difficult it is to support all of the major browsers with a single extension.


Font.js (GitHub: Pomax / Font.js, License: MIT) by Mike Kamermans is a font API for client-side JavaScript. Fonts can be loaded on demand, and an onload callback will run once the font has been loaded:

var font = new Font();

font.onload = function() {
  // The new font has loaded

font.src = '';

Metrics for a font can be accessed. For example, font.metrics.leading will give the line height.

Flavors of JavaScript, 3D Events, LowKick

20 Jan 2012 | By Alex Young | Comments | Tags webgl graphics language ECMAScript testing

Different Flavors of JavaScript

In Different flavors of JavaScript by Lakshan Perera, ECMAScript, ES3, ES5, and ES.Next (Harmony) are explained. Lakshan gives helpful links in context, like, and gives hits on what browser support is available.

For related reading, I touched on some of these areas before in the History of JavaScript series.

DOM Events in 3D Space

DOM Events in 3D Space is a tutorial by Jerome Etienne that demonstrates how to use threex.domevent.js. The 3D DOM event demo shows how this works – click on each teapot and different animations will be triggered. THREE.Ray is used, in particular the intersectScene method, to determine if an event intersects an object.

Jerome has also recently published Boilerplate Builder for Three.js which allows a customised set of HTML, JavaScript, and CSS to be generated that includes everything needed to get started with three.js.


LowKick by E. Azer Koçulu simplifies running tests in multiple JavaScript environments. For example:

./bin/lowkick command ie6 test/config.json

This example, given suitable tools, will run tests in IE using VirtualBox. Headless testing is documented in the project’s readme, but it’ll need a little bit of effort to get it working.

Other drivers can be used, or added through configuration files. For example, LowKick comes with a Node driver, VirtualBox, and “virtualbox-ie”.

Semicolons, Objectively

19 Jan 2012 | By Alex Young | Comments | Tags node style language

Last week in Programming Styles in the Node Community I discussed the styles of three prominent developers in the Node community. The ensuing debate mainly focused on semicolons. Rather than telling you my opinion on the topic, I’d like to explore it objectively so you can make your own decision on whether to use them or not.

Style and semantics aside: how are semicolons handled by minimizers? Is it safe to write a client-side library without semicolons?

Let’s look at the two most popular minimizers according to JavaScript Developer Survey 2011 Results.

YUI Compressor

YUI Compressor can be downloaded as a zip file and includes a jar file in the build/ directory that can be used like this:

java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js --charset utf-8

This basic usage will insert semicolons. If you write a file without semicolons, it’ll remove newlines and insert them for you. It’ll also add semicolons to the end of a file, so concatenating multiple files is safe.

Closure Compiler

Closure Compiler is also a Java application, and there’s a web interface for it at By default semicolons are also inserted, and a semicolon will again be appended to the end of the file to aid concatenation.

In cases where usage is ambiguous, Closure Compiler will raise JSC_SUSPICIOUS_SEMICOLON. Try this in the web interface:

if (true);
else alert('no');

The Closure Error and Warning Reference documentation explains this warning, which can be turned off:

The compiler assumes that this is a mistake, and that you meant to have a statement between if (true) and the semi-colon.

Both of these libraries will also strip unnecessary semicolons. For example, given this simple example:

function hello(longName) {
  alert('Hello, ' + longName);
hello('New User');

The semicolon after alert will be removed.


Standard ECMA-262 and previous versions of ECMAScript include “Automatic Semicolon Insertion”:

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. […] For convenience, however, such semicolons may be omitted from the source text in certain situations.

Prior to this edition of ECMAScript, deprecating or removing this feature was discussed by Brendan Eich and Douglas Crockford in es3.1:semicolon_insertion. Crockford said:

Strong language in a spec is not likely to work. A carrot of better features that induce rewriting works better, but nothing is predictable.

This “carrot” approach comes up frequently in the discussion of ECMAScript’s development, and it can be seen under the list of Themes in the index to the Harmony namespace at the ECMAScript Wiki: harmony:harmony.


When deciding on JavaScript style, the debate on whether to use semicolons will never end. Objectively we can say that:

  • The tools to write client-side code without semicolons exist and are widely used
  • The specification explains how semicolon insertion works, and even includes concrete examples
  • Semicolons are going to stay around for a while

Other languages have optional semicolons. Take Ruby for example: the community at large embraced writing code without semicolons. However, many JavaScript developers learned the language without the benefit of newer tools that help work safely without semicolons. Others are working with server-side JavaScript, running code on one platform rather than multiple browsers and meddling HTTP proxies. They’re able to embrace styles that were previously deemed “unsafe”.

The nature of the language is changing, so expect to see less semicolons in the future.

However, if you like the “machine”-like look of semicolons, and feel more comfortable expressing your intent by using them judiciously, then by all means use them!

Keep your style consistent, and enjoy writing JavaScript.

Node Roundup: 0.7, Cromag, Servitude, Magician

18 Jan 2012 | By Alex Young | Comments | Tags node modules date graphics
You can send in your Node projects for review through our contact form or @dailyjs.

Node 0.7

Node 0.7.0 was announced on the Node blog, which marks the first release of the new unstable series:

Almost all users will want to remain using the stable v0.6 releases

This version includes experimental isolates support. There’s a lot of discussion on this in the Node v0.7.0 nodejs group thread. Ben Noordhuis gave a brief overview:

Isolates are (will be) API-compatible with child processes. So you call child_process.fork({thread:true}) and it will spawn a new isolate instead a new process.

Isolates are something that will crop up a lot more as Node heads towards 0.8, so it’s worth being aware of what they are and how they work.


Cromag (License: MIT, npm: cromag) by Jerry Sievert is named after the fact it doesn’t use monkey patching, unlike a lot of date libraries. We’ve recently seen some extremely solid date libraries, including XDate, and Cromag is another offering with a different API.

Cromag currently offers a slew of methods to manipulate dates and times, which are documented in the Cromag readme. The author has also included tests written with Vows, so it should be fairly easy to hack and patch it.


Servitude (GitHub: JerrySievert / servitude, License: MIT/X11, npm: servitude) also by Jerry Sievert, helps inject CSS and JavaScript into the DOM to cut down on requests. It can also optionally cache requests and mangle them with Uglify.

Jerry’s examples demonstrate Servitude being used with Bricks which is his Node web framework. Servitude’s plugin signature looks similar to Connect middleware, but I don’t think Bricks uses the same API for the request and response objects.


Magician (License: MIT, npm: magician) by Vadim Demedes is an ImageMagick library. The command-line ImageMagick libraries are required, but once they’re installed lots of image manipulation tools are possible from within your Node applications.

jQuery Roundup: Jsonify, jquery-download, jqPagination

17 Jan 2012 | By Alex Young | Comments | Tags jquery plugins json pagination
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.


Jsonify by Jorgen Horstink can serialize DOM elements into JSON through the use of HTML5 data attributes. The two attributes, data-jsonify-name and data-jsonify-getter, are used to map and process values as they’re serialized:

  getters: {
    fruit: function () {
      return this.html().toLowerCase();

Then $('form').jsonify() can be used to process a form with fields like this:

<span data-jsonify-name="food.fruit[0].name" data-jsonify-getter="fruit">Orange</span>:
<input type="checkbox" data-jsonify-name="food.fruit[0].value" value="yes"><br>

<span data-jsonify-name="food.fruit[1].name" data-jsonify-getter="fruit">Banana</span>:
<input type="checkbox" data-jsonify-name="food.fruit[1].value" value="yes"><br>

<span data-jsonify-name="food.fruit[2].name" data-jsonify-getter="fruit">Strawberry</span>:
<input type="checkbox" data-jsonify-name="food.fruit[2].value" value="yes"><br>


jquery-download demo

jquery-download (License: BSD, Demo: jquery-download demo) by John Krauss allows the DOM, or parts of it, to be downloaded through data: URIs. The author’s demo has some SVG, so this plugin could be used to extract and download SVG as it’s edited.

The same author has a load of new stuff on GitHub at


jqPagination screenshot

jqPagination (GitHub: beneverard / jqPagination, License: GPL v3) by Ben Everard is a different interface for pagination: next and previous selectors are displayed, but if the current page text is clicked then a page number can be entered. A callback gets the current page number when it’s changed:

  paged: function(page) {
    // do something with the page variable

In the callback, anything could happen: the current location could be changed, or an Ajax method could load more content from a RESTful API. The author has included CSS as part of the plugin’s source.

JavaScript Patterns, jQ.Mobi, Third-Party JavaScript

16 Jan 2012 | By Alex Young | Comments | Tags third-party books patterns mobile

JavaScript Patterns

The JavaScript Pattern Collection (GitHub: shichuan / javascript-patterns) by Shi Chuan is a collection of JavaScript patterns and anti-patterns including jQuery, functions, object literals and constructors, and object-oriented design patterns.

The jQuery patterns and anti-patterns are pretty good reading for anyone who writes plugins. The reasons for anti-pattern status are usually cited with a link to a post that covers why the technique is considered poor form. For example: universal-selector.html and requery.html.


jQ.Mobi (GitHub: appMobi / jQ.Mobi, License: MIT X11) from appMobi is a framework aimed at HTML5 mobile browsers. According to the documentation, jQ.Mobi, is the “query selector” library. I thought this was confusing because surely a web framework that targets WebKit only has to wrap a small amount of functionality around querySelectorAll? In reality, this library includes most of what jQuery does: $.map, $.each, $.fn, css and attribute getters and setters, event handling, and even Ajax methods. There’s also a user interface library, and plugin support.

Each part of the library is presented as a monolithic file, and it doesn’t look like they’re built from smaller files. If this was my library, I’d split each of these files up into modules, with a build process that can output monolithic and minimised files. I’d also consider using the Asynchronous Module Definition specification to structure the library. It’s also slightly difficult to find the source – the homepage has a button that prompts for an email address, with a greyed out link to “No thanks, just get the code”.

The jQ.Mobi site claims that this is a rewrite of jQuery that targets WebKit mobile browsers, but we’ve already seen this several years ago at this point with libraries like Zepto. And there are also mobile interface libraries that are compatible with both jQuery and Zepto, like jQTouch, so I question the wisdom of coupling a jQuery clone with an interface library.

Writing Third-Party JavaScript Tutorial

Writing Quality Third-Party JS by Rakesh Pai from {errorception} is a tutorial series about writing third-party JavaScript. If you’re looking to scripts that run on other sites, like Disqus or Google Analytics, then this is an interesting read that covers the basics.

Third-party JavaScript The Book by Ben Vinegar and Anton Kovalyov can also be bought as part of Manning’s Early Access Program for $35.99. It looks like it’ll be the most thorough coverage of this topic once it’s finished.

Screenshot App, twss.js, WebGL Tunnel Tutorial

13 Jan 2012 | By Alex Young | Comments | Tags node webgl graphics

Screenshot App

Screenshot App (License: MIT) by TJ Holowaychuk is an Express app that uses PhantomJS to generate screenshots of web pages. It’s a small web service with a simple API that’ll return a PNG when called with a URL:

curl -# http://localhost:3000/ >

This is a nice little example of an Express app. Like TJ’s other public apps, this one is structured around a lightweight app.js file, and then the HTTP methods are split into files in routes/. TJ defines the Express app object as a global, which means it’s visible inside the routes modules. There’s a lot to learn here if you’re an eager Express developer.


Earlier this week a reader sent in twss.js (npm: twss, License: MIT) by Daniel Rapp, a classifier for Node that determines if a string can be replied with “that’s what she said”. I actually took a look at how it works, and rather than being a simple joke module, the author has actually attempted to build a K-fold cross-validation classifier. What’s even more hilarious is the module currently has 457 GitHub watchers.

The module includes data, so it’ll classify things out of the box:"You're not going fast enough!");   // true

If you’re interested in classifiers, there are a few for Node – we’ve featured brain (npm: brain, License: MIT) by Heather Arthur before.

WebGL Tunnel Tutorial

I recently mentioned a WebGL tunnel effect created by Jerome Etienne. He’s now published his tutorial that explains how the demo works, in Tunnel Effect for Your Demo. By “demo” the title is referring to demoscene demos.

Amazingly, the source for the effect is only 20 lines.

Programming Styles in the Node Community

12 Jan 2012 | By Alex Young | Comments | Tags node style language

Programming style is where opinion and reason collide, often leading to endless arguments with no clear winner. The real secret to style is to be consistent, and respect other people: if you’re working on a project by another author, then follow their style. Some companies and open source projects may have their own in-house style guide, but it’s generally fairly easy to pick up the style of a project after browsing the source for long enough.

Why do I bring this up? Well, the Node community has several dominant styles, each with their own advantages. If you’re starting your own Node projects then you might like to use the style of a well-known developer for guidance of inspiration.

TJ Holowaychuk

TJ Holowaychuk, author of Express, Jade, Stylus, and Mocha, has an extremely consistent style. TJ uses two spaces for indentation, semicolons to end lines, and formats commas at the start of a new line:

var connect = require('connect')
  , Router = require('./router')
  , methods = Router.methods.concat('del', 'all')
  , middleware = require('./middleware')
  , View = require('./view');

This practice is becoming more common in JavaScript, and the arguments for and against it vary. Isaac Z. Schlueter created a gist on the subject at which grew into a heated debate that currently has 64 comments (since April 2010). It’s worth noting that TJ also carries this over to his JSON formatting, and generally only uses one var statement at the top of each new scope.

One of TJ’s more subtle stylistic habits is to remove the space before the opening brace of a method:

exports.parseQuality = function(str){
  // ...

When writing a function, however, he’ll usually retain the space:

function quality(str) {
  var parts = str.split(/ *; */)
    , val = parts[0];

  var q = parts[1]
    ? parseFloat(parts[1].split(/ *= */)[1])
    : 1;

  return { value: val, quality: q };

Notice that the ternary operator is split across new lines as well, which can make longer expressions more readable.

TJ also uses double quotes sparingly as well. Most strings are written using single quotes.

TJ’s Luna project isn’t related to JavaScript, but during development he made an interesting point: he likes the “machine-like” look of JavaScript’s operators. Some languages provide and instead (or as well as) &&, but TJ prefers the way operators like && read:

Personally I’m not a fan of “wordy” operators, aka or and for || &&, etc, though again they can look ok within reason, but I find the “machine-like” look of operators provide a nice visual separation.

I think the “visual separation” comment is interesting, because I find semicolons in JavaScript also provide visual separation, although other people may find this noisy.

 Isaac Z. Schlueter

Isaac Z. Schlueter, author of npm (where would we be without it?) is outspoken about style and language “magic”. I enjoyed reading his post Experts, Idiots, and Taste, and JavaScript is Not Web Assembly is related to this. Isaac doesn’t like language “magic”. He’s pragmatic and keeps things simple. Let’s take a look at his coding style.

Like TJ, Isaac uses leading commas and two space indentation, and usually groups variables under one var. Functions are spaced out like this: function help (args, cb) {.

The biggest difference between TJ and Isaac is semicolons. Isaac doesn’t use them to end lines unless necessary:

function checkGit (folder, cb) {
  // if it's a git repo then don't touch it!
  fs.lstat(folder, function (er, s) {
    if (er || !s.isDirectory()) return cb()
    else checkGit_(folder, cb)

Isaac has written a detailed explanation of this decision in An Open Letter to JavaScript Leaders Regarding Semicolons. He makes good points as to why semicolons can be left out, although admits that this style may only be slightly superior.

Ryan Dahl

Ryan Dahl, creator of Node, has a slightly different style yet again. Like TJ and Isaac, he uses two spaces for indentation. Conversely, he’ll use multiple var statements per-line:

var express = require('express');
var socketio = require('');
var bench = require('./bench');

This is from d3bench, a small benchmarking app. He writes functions and methods with a single space: io.sockets.on('connection', function(socket) {. He doesn’t line up colons in object literals:

var r ={
  url: "http://localhost:8000/buffer/12345",
  concurrency: 10,
  requests: 50000

Deeper Style Choices

Indentation, semicolons, and other formatting topics are one side to coding style. Another is the choice of language features. Both TJ and Isaac use exceptions sparingly. Isaac is outspoken about exceptions:

I have settled in the last few years on the sentiment that try/catch is a mistake, and an anti-pattern.

This is from try/catch/throw on the Node Google Group.

TJ sometimes organises code into folders, with an index.js file, so a group of modules can be loaded with one require. Router in Express is an example of this. This technique removes a barrier from splitting up code into multiple files. Both authors also make heavy use of other Node modules, rather than reinventing their own solutions to common problems. The npm package.json is a good example of this.


Before starting a new project, it’s worth researching the programming styles used in the community of your chosen language and framework. Encourage the rest of your team to be consistent, even if they’re freelancers or contractors who have their own styles. The best comment I’ve read on how important it is to respect a project’s style is by Isaac:

In your own house, you get to decide the styles and customs. But be aware that it might have an effect on who feels comfortable at your parties.

Yes, it’s all FUD, in reply to Actual drawbacks to omitting semi-colons?.