Scripting with NodeJS
Scripting, NodeJS & MelbJS
In addition to building applications, NodeJS can also be used as a powerful scripting language to build utility scripts to automate daily or repetitive tasks, and provide support scripts for web applications (ie, automate deployments or to fix inconsistent database content). It's a powerful combination being able to write front end websites, backend servers and support/utility scripts all in the one language, syntax, style conventions, and methodology - especially helpful when developing rapidly in teams.
Our NodeJS Scripts
Internally we have a wide range of utility scripts written in NodeJS, many are small and simple with a specific focus on what they are trying to achieve. That's important, don't let any individual script try to do too much.
For example when building iPhone/iPad applications, we use NodeJS to build, package and upload our apps to TestFlight (a beta testing SaaS). The deployment script took a morning to write and refine and is under 100 lines (most of which is defining configuration). Having this script allows us to run one command`node build.js`and in minutes our app is up on TestFlight ready for our beta testers to download. Multiply the time saved from not having to perform the steps manually over all our projects with the number of times we release betas (the more the better is the agile approach), and you fast begin to see the value these scripts can provide. On top of this comes consistency from not forgetting certain steps, and the ability to have any member of the team run it.
Presentation at MelbJS
At the April MelbJS meetup I presented to share this passion for writing scripts and the power that such scripts can bring, in both creatively solving problems and increasing your efficiency through automation.
Below are the slides and code examples demonstrated at the meetup.
Three code examples were presented on the evening. The examples demonstrate how to quickly you can write very simple scripts and cover topics such as; how to daemonize, run from your bin folder, use command line arguments, interact with filesystem, mongodb and scraping websites.
To run the examples, make sure you have node and npm installed, then `cd` into the directories and simply run `npm update` and then `node app.js` to kick off the main script.
Installing NodeJS & npm
There's a whole bunch of ways to get NodeJS and npm installed. We like to use the n version manager which makes it super simple to switch between node versions (although most of the time you'll want to stick to 'stable' release).
The latest versions of node come with npm, if you're going back to old ones or having issues, try the following install shell script
- Node modules wiki page lists out a whole heap of modules to get you going with your scripts/projects.
- Checkout Marc Fasel's presentation at OSDC last year. Marc is a MelbJS regular and shares his experiences with using NodeJS in a team.
- Checkout the work being done by the LearnBoost team on GitHub - they are awesome contributors to the NodeJS community and their modules empower a lot of our apps.
- And of course, if you are in Melbourne, come to MelbJS!