Why I love Node.js part 2…

I published a blog post about Node.js a couple of years ago. After recently using it for some more crazy stuff, I thought I’d post more reasons I love Node.js!

Promises promises

I mentioned in my previous post about bracket hell. Well in the intervening period I’ve started using Promises for a lot of things, especially the Q promises library in npm.

Promises allow you to easily chain actions together. You can run things in parallel and wait for them to finish, or runs things in sequence, or both! I’ve just used both in the last week to start 10 ‘threads’ using Q.all, with each thread running 1500 tasks in series using promisesArray.reduce.

This allowed me to test a customer’s Node.js Express based web application at scale with just a few lines of code, which in turn tested high throughput ingest and query loads in MarkLogic Server.

Express web apps

Creating a web server sounds scary, but with Express is very, very easy. You just intercept the small number of routes (URL patterns) you need, and you’re away!

You can even use the URL pattern itself to infer parameters. E.g. /get/fred/1 could be used to fetch the first document in the fred collection. Pretty cool, and reminiscent of Ruby on Rails.

I’d definitely recommend learning Express first if developing a web application.

Command line applications

Did you know you can even use Node to create an NPM package that installs a global application on any platform? This is AWESOME in NPM form!

I’ve used this to package up two tools I use on projects in to my mlnodetools package. mljsadmin allows you to capture a MarkLogic application, with all server settings, and transfer it to another machine with just a couple of commands. mljsserve uses Express mentioned above to create a thin web server which also proxies MarkLogic REST API urls directly to MarkLogic Server.

This is great for demonstration web apps at work in MarkLogic Sales Engineering (We’re hiring BTW so please contact me if you’re interested!)

I use a few NPM packages for mljsadmin:-

  • minimist is used to parse command line arguments. I can’t express my love for this capability enough!
  • q again is used for promises when chaining complex functionality together (so ‘install’ runs about 9 sub-commands in series, which can also each be ran on their own from the command line)
  • winston is used for logging output and errors to both the command line (info level) and a file (debug level). I use this in my mljs MarkLogic REST API wrapper too.
  • istextorbinary allows me to test files to see if they are binaries. Useful as MarkLogic Server doesn’t hold every mime type in use. I use this to determine whether I need to do a binary upload or not, and what mime type to specify
  • terminal-kit is used to provide colour coded and styled text on the command line output. Useful so I can have green for successes, yellow for warnings, and red for major errors


I’m constantly and pleasantly surprised that for whatever little tool or demonstration I need, no matter how complex, Node.js greatly simplifies my life!

Node.js‘ event driven nature and wide support for extension via NPM allows me to quickly find and make use of even the most esoteric functionality I need. I often have a choice of esoteric libraries!

I’d recommend learning Node.js to anyone working in IT today. Node.js can make your projects fly!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s