Optify’s Favorite Node.js Utilities

While Optify is primarily a Java shop, we often take the opportunity to learn and evaluate new languages and tools when building small, non mission-critical components. Over the past few weeks, @c_hundley, @tommyunger and I have been using Backbone.js, Nodejs, Expressjs, and MongoDB to automate the creation of several reports that had previously been built (tediously) by hand, and present these reports and KPIs in an internal company dashboard (more on this project in a future post). Since none of us had any prior experience with Node or Express, we spent a fair amount of time learning these frameworks by researching libraries, reading blogs, and relentlessly refactoring when we decided we had gone down the wrong path. Along the way, we found several libraries that have proved to be extremely helpful.
Async.js
Async.js provides several utility functions for working with asynchronous JavaScript. There are the typical set-based functions you’d see in many functional programming languages (map, reduce, filter, etc), as well as functions for async control flow. The primary benefits we’ve seen from using async.js is that it’s really helped to force us out of imperative thinking and into solid functional patterns for control flow. In addition, the control flow methods: parallel, series, forEach, etc help make async control flow explicit.
As mentioned in the documentation, async.js can also be used in the browser. I’ve posted an interactive demo of some async.js features on Github.
Mongoose
Mongoose is a flexible, schema-based, ODM (object-document mapping) tool for Nodejs and MongoDB. Mongoose allows you to easily persist and load data from MongoDB based on a defined object schema. The example below defines a schema and model for an Incident (which in this case represents an event pulled from PagerDuty), creates an instance of the incident model, and persists the model to MongoDB.
var mongoose = require('mongoose'); var Schema = mongoose.Schema, ObjectId = Schema.ObjectId, Model = mongoose.Model; var IncidentSchema = new Schema({ incidentNumber: Number, createdOn: Date, subject: String, status: String, lastUpdatedOn: Date, lastUpdatedBy: String }); var IncidentModel = mongoose.model('Incident', IncidentSchema); var incident = new incidentModel.IncidentModel({ incidentNumber: 12345, createdOn: new Date(), lastUpdatedOn: new Date(), lastUpdatedBy: 'Nathan', subject: 'Server example.optify.net is unreachable', status: 'Open' }); incident.save(function(err) { // You'd generally invoke a callback function here passing err if an error occurred });
Restler
Last but not least is Restler, a fantastic HTTP client library for Nodejs, which hides much of the complexity of using Node’s http client. A few of Restler’s key features:
- Serialization of post and querystring data
- Automatic deserialization of XML, JSON and YAML responses
- Handles basic auth
A quick example using Restler to hit Twitter’s legacy search API. This can be run directly in the Node REPL as long as you install Restler via NPM first.
var rest = require('restler'); rest.json('https://search.twitter.com/search.json?q=optify') .on('complete', function(response) { console.log(response); }) .on('error', function(err) { console.log('An error occurred:' + err); });
We’ve found the following libraries invaluable for quickly building maintainable code, and since this project has primarily been an off-hours effort, speed is essential. Do you have any libraries you prefer in addition to, or instead of Async, Mongoose, and Restler? We’d love to hear about them in the comments!









Pingback: Useful Node.js Utilities « Nathan Harkenrider