How cool is Node.js?

An introduction to Node.js

Node.js is part runtime environment and part library for building network applications using server-side JavaScript. It uses Chrome's JavaScript runtime engine to execute JS code directly without the need for the browser sandbox.

Basically, that means you can write the same language (Javascript) on the Client and Server side. Neat huh?

Installing node is simple using Homebrew (If you're using a Mac):

brew install node  

Once Node is installed you have access to it's pretty nifty package manager.

Using NPM you can install a tremendous array of things. From basic code linting and automated task managers to fully fledged JS templates.

I'm going to show you one of these cool features now.


We're going to create a PDF from an HTML page with the command line using PhantomJS.

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

Lets go ahead and install PhantomJS. This can be done with the following command in terminal.

npm install phantomjs  

That will install PhantomJS and all it's necessery dependencies.

Since PhantomJS is using WebKit, a real layout and rendering engine, it can capture a web page as a screenshot. Because PhantomJS can render anything on the web page, it can be used to convert contents not only in HTML and CSS, but also SVG and Canvas.

The following script: github.js, demonstrates the simplest use of page capture. It loads the Github homepage and then saves it as a PDF.

var page = require('webpage').create();'', function() {  

Then run the following command in Terminal:

phantomjs github.js  

This can also be done on the command line using the rasterize.js file included in the PhantomJS examples. Which gives you a few more controls over the previous example out of the box.

phantomjs rasterize.js '' github.pdf  

It's that simple.

Let me know what you think in the comments below…