I’ve been a fan of Node since we were first introduced in 2011. Unfortunately, it’s taken me a few years to come up with a compelling business use for Node.
Why so long?
When I first met Node, I was working at a .Net development shop. Node was still a Linux-only tool, so to use it on a Windows machine I needed to compile the binaries myself (at the time, using a very exotic development environment).
It worked, but was messy and made my co-workers nervous when I suggested using something they couldn’t build on their own (or find reliable 3rd party support for) in production. I tabled Node for later and wondered if I’d ever be able to use server-side JavaScript in my projects.
Working with a WordPress shop gives me more flexibility to use open source (i.e. community “supported”) software, so I’ve long hoped I could find some synergy between Node and WordPress. On the whole, though, I haven’t; much of the interactions I’ve seen thus far are crude hacks or developers merely trying to find an excuse to use Node just so they can use Node.
I spoke with some Node developers at length this past weekend at a conference trying to find ways I can supplement my WordPress work with Node and, finally, came up with some workable project ideas.
Using Node in development
I already use Node in day-to-day development thanks to Grunt. My build system is scripted in JavaScript[ref]I originally scripted everything in Ruby, using Rake, but converted it to Grunt as few developers were willing to learn yet another language in order to support my projects.[/ref] and uses both Node and NPM to keep things up-to-date in the background.
But a build tool doesn’t feel like “using” Node, so I’ve been searching for other ways. Today, a specific project came up that dove-tailed perfectly with Node.
I’m working on a project where I need to verify – in bulk – the “source” parameter of several messages on Twitter. This is easy enough to do thanks to Twitter’s REST API, but the REST API itself isn’t necessarily easy.
Every request needs to be signed (OAuth) and the data return contains a large chunk of data extraneous to my interest. I wanted a simple way to grab the data, strip out just what I need, and dump it to a report. As it turns out, this is a perfect task for a Node application.
I built a simple JavaScript program – the entire thing is 10 lines of code – that takes care of my needs. It first makes a request to Twitter for the data (using the node-twitter library). Then it iterates through the results and parses out just the pieces I need (using vanilla JavaScript). Next it formats the raw JSON data to a less markup-like format to make a quick view easier (thanks to the prettyjson library). Finally, it writes the entire report to a text file for simple distribution (using native Node APIs).
It’s not a websocket server, a dynamic blog engine, or anything fancy on the server-side. But it is a way to use Node on the client side to quickly parse what is otherwise a large body of data and rapidly distribute results to my team.
Whether it’s build tools or simple tools like the one above, being able to run JavaScript on my machine outside of the browser means I can be far more productive than without it. How else might you use Node in your development cycle?