The JavaScript blog.


node modules express amazon grunt foundationdb

Node Roundup: Fowl, grunt-ec2, connect-body-rewrite

Posted on .


Fowl (GitHub: OptimalBits / fowl, License: MIT, npm: fowl) by Manuel Astudillo is a document and query layer for FoundationDB. It provides a similar API to NoSQL databases like MongoDB, but has support for multidocument transactions:

Transaction support is an incredibly powerful feature that simplifies server logic and helps avoiding difficult to solve race conditions.

Fowl provides a low level API based on keypaths for describing documents and its properties following CRUD semantics.

It includes tests and each API method is documented in the readme file. Basic usage looks like this:

// Open a foundationDB database

// Create a document (if _id not specify a GUID will be generated)
var john = fowl.create('people', {  
  _id: 'john',
  name: 'John',
  lastname: 'Smith',
  balance: 100

// Use transactions to transfer money from one account to another
var tr = fowl.transaction()

tr.get(['people', 'john', 'balance']).then(function(johnBalance) {  
  tr.put(['people', 'john', 'balance'], johnBalance - 10);


grunt-ec2 (GitHub: bevacqua / grunt-ec2, License: MIT, npm: grunt-ec2) by Nicolas Bevacqua is a set of Grunt tasks for creating, terminating, and deploying Node applications to AWS EC2 instances.

The deployed Node applications are served from behind an Nginx proxy. The task reference explains what each task does -- there are quite a few.

It supports most of the things you want to do when setting up Node applications, including SSL, SSH keys for each instance, rsync support for fast and painless uploads, and hot code swaps.


There are times when the logic of my Node web applications have seemed to need the response body to be rewritten, but in the middleware rather than the main route logic. The connect-body-rewrite (GitHub: rubenv / connect-body-rewrite, License: MIT, npm: connect-body-rewrite) by Ruben Vermeersch makes this possible. The examples use regular expressions to replace text, based on the request headers:

  accept: function (res) {
    return res.getHeader('content-type').match(/text\/html/);
  rewrite: function (body) {
    return body.replace(/<\/body>/, "Copyright 2013 </body>");

I like the way it's designed to use an accept callback, because it makes it easy to see what the rewriter actually does by keeping the logic close together.


node modules express mongo middleware amazon

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

Posted on .

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 nodejs.davidherron.com/. 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.


hosting node servers ec2 amazon

Node Deployment Recipes

Posted on .

During the publication of our Let's Make a Web
tutorial series, people have
been posting comments about hosting Node apps. In particular, the
comments on Node Tutorial
are quite detailed.
In this article I've summarised Node hosting approaches so you can pick
the approach that suits you.

In the wider web development community there are some amazing
plug-and-play solutions for hosting web apps. Hosting Node apps isn't
quite as easy, yet, but there are some excellent solutions that are both
fast and easy to understand.


Upstart is a replacement for the /sbin/init daemon. You can use it to start, stop, and
restart Node processes. If you're using Ubuntu you should already have
it, if you're using Debian or another Linux distribution it's fairly
easy to install. It actually comes with replacements for the
sysvinit scripts that come with Debian. The Upstart
Getting Started
has more details.

This is an upstart script in /etc/init/myapp.conf that I
knocked up to manage a Node app on EC2:

description "MyApp"
author      "alex"

env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

start on runlevel [23]

    export NODE_ENV=production
    exec /usr/local/bin/node /data/apps/MyApp/app.js 2>&1 >> /var/log/node.log
end script

The respawn
instruction will restart my app if it dies for some reason. There's also
a respawn
which can limit respawning based on a timeout.

On a Debian/Ubuntu server I'd typically use the start,
stop, and status commands to manage this

$ sudo status myapp
myapp start/running, process 9717

Alternative: Monit

I've also used Monit with great success.
Monit can monitor processes and restart them when required. It's
possible to restart a process based on CPU usage or memory.

Monit relies on a PID file, so you may need to write a wrapper script
around your Node app. There's an example in Monit's
. Cluster can write out PID files.

In addition to many other features, Monit can be managed with a web
interface and send out email alerts.


To ease the management of your application further, and potentially
better support multi-core servers,
Cluster can be used. Apps have to be set up to use Cluster, but it's very straightforward.

This is an Express example I've used before on DailyJS:

var app = require('express').createServer(),
    cluster = require('cluster');

app.get('/', function(req, res){
  res.send('hello world');


Cluster has many interesting features, from zero-downtime reloading to a
commnad-line console for real-time administration.

I asked TJ Holowaychuk how he manages production Node apps, and
apparently he's using
Cluster evolved from an older project called Spark, so it seems like
it's got some pedigree.


The age old problem of hosting web software is port 80: binding to this
port requires root. There are ways around this: some firewalls can
redirect traffic from port 80 to a non-privileged port. An alternative
to this is authbind, which can be easily installed in many
distributions. In Debian it's just a case of apt-get install

Once it's installed, create a file in
/etc/authbind/byport/80 and chown it to the
user you want to run your Node app as. Then, if you're using Monit or
Upstart, make sure your Node process is launched with
authbind and run it as that user.

The previous Upstart example would have to be adapted to work with

description "MyApp"
author      "alex"

env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

start on runlevel [23]

    export NODE_ENV=production
    su -c 'authbind /usr/local/bin/node /data/apps/myapp/app.js 2>&1 >> /var/log/node.log' www-data
end script


Almost a year ago I wrote a tutorial on using
nginx to host Node apps. Servers like Apache and nginx can sit in front of your app and the
web. This means you don't need to run on a privileged port, and the web
server can do jobs it's good at like serving static files.

This may fit in better with your current hosting set up. You could still
use a service monitor with Cluster to manage the Node processes.


I deployed my Upstart/Authbind/Cluster combo to EC2 without any trouble
at all. I used the AWS Console to
create EBS storage devices to keep my Mongo database, application, and
static assets. After that it's basic Linux sysadmin.

Just remember to add your IP address and port 22 to the security policy,
else you won't be able to connect!