A Few Node.js Essential Modules
Friday, 3rd August 2012, 14:40
Actually, I take that title back, I don't believe any Node.js module is essential. And I'm beginning to worry about how the whole development side of things is being taken over by people obsessed with frameworks that are entirely about making Node apps easy to develop, whilst doing their best to destroy all the beauty and speed of Node.js in the first place.
If this is you, please leave Node alone and go get a job at Microsoft.
You know I'm even thinking seriously of creating my own replacement for Connect (a framework I actually like) which tries to re-address the balance between usability and speed. But that is a job for next week, until then, here are a few modules which you should check out.
Coloured
If you develop with Node, you will spend an awful lot of time looking at a console terminal, and console.log lines can get very busy down there. The solution to making it more readable is Coloured, and even spelt properly too!
How it works is cute, it extends the String object, adding functions which insert all the correct control codes for adding colour and style to terminal logs. You can also choose not to extend the String object if you wish, and extend either a different object or no object at all and just use it as a function.
So you can either do it this way (my preferred option):
var colour = require("coloured");
colour.extendString();
console.log("Error: ".red().bold() + "Something Bad Happened!".yellow());
Or you can do:
var colour = require("coloured");
console.log(colour.colourise("Bad Things are Happening :(", { foreground: "red", background: "white", extra: "bold" }));
There are two shorter ways:
var colour = require("coloured");
console.log(colour.colour("Error Error Will Robinson", "red"));
and
var colour = require("coloured");
console.log(colour.extra("I'm Feeling Quite Strong", "bold"));
Using this sure does make things easier to see in the console, especially if you spam different debug messages in different colours. You can quickly spot the things you are most interested in seeing.
Less
Perhaps this shouldn't need any introduction, it is a must for anyone who works with Cascading Style Sheets, which lets face it is going to be 99.999% of web developers. It pretty much cures everything that is annoying about CSS without losing any compatibility with CSS. In much the same way CoffeeScript is a bad idea, Less is a good one. It gives you variables, macros, nesting and includes, and the result is compiled into valid CSS.
Quick example:
@base-font-size: 1.1em;
p
{
font-size: @base-font-size;
}
h2
{
font-size: @base-font-size + 0.2em;
}
I'm sure you can guess the rest, more examples on the website. This module is best used as an automated way of compiling your CSS at the server, it has the added benefit of compressing it a bit too, always a good thing.
UglifyJS
Another important tool is UglifyJS, which sounds a lot harsher than it is. Basically this is the all important Javascript compressor, a must have for delivering mini-fied .js files to the client.
var ugly_pars = require("uglify-js").parser;
var ugly_proc = require("uglify-js").uglify;
var orig_code = "Javascript code to send to the client";
var ast = ugly_pars.parse(orig_code);
ast = ugly_proc.ast_mangle(ast);
ast = ugly_proc.ast_squeeze(ast);
var minified_code = ugly_proc.gen_code(ast);
Much like Less above, this is best used in automated methods for compiling client side Javascript. Note that Connect actually has modules which will integrate both UglifyJS and Less together, delivering compressed CSS/JS without much effort on your behalf.
Nodemailer
Many a website needs to send an email, and I've been using reliably Nodemailer for some time with no issues. It has just the right combination of ease and functionality. If you need to send mails, then this is a great module to start with.
An example should explain all:
var email = require("nodemailer");
email.SMTP = {
host: "mymail.server.com",
port: 25,
}
email.send_mail(
{
sender: "from@whenceitcame.com",
to: "some@address.com",
subject: "Thank You!",
body: "Just to say thanks for your comment!\n"
},
function(error, success) {
if (error)
console.log(error);
}
);
I like how the configuration is separate from the email sending, gives you options, and it's always good to have options. For the record, it does support HTML, SSL/STARTTLS, connection pooling, attachments, and much more.
Node-imagemagick
Finally, processing uploaded images, or making dynamic ones, is often nicely achieved with ImageMagick, so what better way to manipulate them on the server than with Node-imagemagick? Ideal for making thumbnails, etc.
Again a quick example:
var magick = require("imagemagick");
magick.convert([strSourcePath, "-trim", "-resize", "75x75^", "-gravity", "center", "-extent", "75x75", "-quality", "65%", strDestThumbPath], function(err, metadata) {
if (err)
console.log(err);
else
{
console.log("Woop!");
}
});
These are not the only modules I use, I dabble with others, however hopefully they fill basic gaps for someone new to Node.js who wants to know where to start, without having a "This is how you write Node Apps" philosophy thrust upon them by someone who thinks any such thing is set in stone.