My favorite node.js packages 1

I have spent the last year programming almost exclusively in javascript using the node.js framework. Previously, my client side code was written in javascript (of course) and the server side code in C#. Having different languages and frameworks on both ends required more “plumbing” in the form of code that connects the two parts together. Surely there are ways to reduce the overhead and bridge the two sides, such as ScriptSharp, but wouldn’t it be more convenient if both sides work the same way?

For me, the main benefit of using node.js is coding with javascript on both ends. There are other pros (event model) and cons (performance and stability), but regardless of all that, using javascript on the server side is the one that brings me a lot of enjoyment. I want to add that I am not a “religious” person when it relates to computer languages and frameworks. In fact, I am very pragmatic, whatever works (for you), works.

nodejs-logo

With this introduction in mind, those who use node.js know that node is a relatively thin framework. The core API provides only basic capabilities and the developers are expected to use packages (AKA libraries or modules) to enrich the framework. By convention, node packages are managed in the npm Public Registry, which is a public instance of the npm open source project, where everyone can publish packages. Since there is no single company coordinating published packages in the eco system, it is common to find many packages doing the same or similar things. One observing the system can see a type of “survival of the fittest”, where certain packages in a category will become the popular ones and then become the “de facto standard” in the category.

Here are some packages that I like the most and some pseudo-example of using them:

async

This would be the must-have package for managing flows and other business logic. Due to node’s event base design, code can get nested very quickly and it is imperative to think of your execution logic in the context of design patterns such as: queue, sequence, parallel, etc. Async implements those patterns and more leaving you with clean, readable code.

var async = require('async');
function downloadUrls(urls, next) {
  var concurrency=5;
  function downloader(url, next) {
    mock.httpGet(url, function(err, data) {
      if(!err) next(err);
      else mock.saveUrlData(url, data, next);
    });
  }
  async.eachLimit(urls, concurrency, downloader, next);
}

mongodb

This would be my default db client package. Node and mongodb work well together. Mongodb is json based and naturally fits the event based model of node. Mongodb’s client for node is well written and documented.

function insertDocuments(db, callback) {
  db.collection('documents').insert([
    {a : 1}, {a : 2}, {a : 3}
  ], function(err, result) {
    assert.equal(err, null);
    console.log("Inserted 3 documents into the document collection");
    callback(result);
  });
}

express (with jade and stylus)

This would be the “stack” of choice for web (or rest) server implementation. Really minimal by design just like node.js itself. Supporting an eco system of packages of its own. Works well with jade or other template engines as well as with stylus or other CSS pre-processing engines.

var express = require('express');
var app = express();
app.get('/', function(req, res){
  res.send('hello world');
});
app.get('/user/:id([0-9]+)', function(req, res){
  res.send('user id: ' + req.params.id);
});
app.listen(80);

restler

This would be a convenient package to use if you need to consume a rest service. Nothing extraordinary, just cleaner and faster that using the core API directly.

var rest = require('restler');
rest.get('http://google.com',{timeout: 10000}).on('timeout', function(ms){
  console.log('did not return within '+ms+' ms');
}).on('complete', function(result) {
  if (result instanceof Error) {
    console.log('Error:', result.message);
    this.retry(5000); // try again after 5 sec
  } else {
    console.log(result);
  }
});

moment

Anybody working with javascript knows that date and time are a pain with the built-in Date object. “moment” is a real gem that makes it very easy and clean to do everything you can ever want to dates and times. You can parse them, format them, calculate relative times, subtract, etc. This package is also usable in the browser.

var moment = require('moment');
var a=moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
a.seconds(0);
a.format("dddd, MMMM Do YYYY, h:mm:ss a");

var b=a.startOf('quarter');
var dur=moment.duration(a.diff(b));
dur.humanize();

minimist

This would be a convenient library to parse command line arguments. “minimist” knows to parse standard unix parameter format. Knows: one letter parameters, keywords, aliases, free text, etc.

var argv = require('../')(process.argv.slice(2));
console.dir(argv);
// $ node example/parse.js -x 3 -y 4 -n5 -ab --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  beep: 'boop' }

One comment on “My favorite node.js packages

  1. Reply bruce banner May 14,2015 4:24 pm

    Pretty shocked you didn’t include cool-ascii-faces!

    https://github.com/maxogden/cool-ascii-faces

Leave a Reply

  

  

  

This site uses Akismet to reduce spam. Learn how your comment data is processed.