Software Alchemist

I am constantly seeking answers and ways of transforming desires into reality by coding

NodeJS + MongoDB = TinyURL

| Comments

Last week I started playing with NodeJS

NodeJS is an evented IO for V8 JavaScript. It uses asynchronous processing for all operations. If you want to open file, you pass in the callback function as argument to file open function, which then gets executed when the file is opened and available.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// we will use sys.log for logging
var sys = require('sys');
// send open file command, with callback
fs.open('path/to/file.js', 'w', 0777, function(err, fd) {
  //throw error, if there was one
  if (err) throw err;
  // send write command with opened file, content, starting point, encoding and callback
  fs.write(fd, 'Hello World!', 0, 'utf8', function (err, written) {
    //throw error, if there was one
    if (err) throw err;
    // after successful file write, log number of bytes written
    sys.log('Success! Wrote ' + written + ' bytes');
  });
});

The aforementioned approach leads a much higher number of requests with a much less execution and, most importantly, wait times. And since its all JavaScript, we’re not surprised, because that is how JavaScript essentially works in the browser window. Think about it, the browser needs to re-draw its ui every time you a change occurs, staying responsive to user interactions at the same time, the only way to do this is by executing event/callback loops.

So where would you use NodeJS. In real-time data processing. The perfect example of such usage is HummingBird, which uses NodeJS and MongoDB for producing real-time usage analytics and statistics for Gilt Groupe website, you can fork it on github.

Now I was installing NodeJS under Cygwin which was a bit tricky at first (the python version that comes with Cygwin needed to be “rebased”).

To run a rebase command on Windows, you need to close all Cygwin processes, open C:\cygwin\bin\ash.exe, and run:

1
/usr/bin/rebaseall

In order to compile NodeJS, you would need the following: * python * OpenSSL * Make * libg++ compiler * NodeJS source

After all the above dependencies were installed, cd /path/to/your/nodejs, and run:

1
2
3
./configure
make
make install

To run tests, execute:

1
make test

Now that you have NodeJS installed, the first step is to create a “Hello World!” application, to do this, create example.js with the following content:

1
2
3
4
5
6
7
8
9
// http package is used to create http servers
var http = require('http');
// create server that will write string 'Hello World' with a new line on every request to ip 127.0.0.1, port 8124
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
// Log message after createServer is successfully sent
console.log('Server running at //127.0.0.1:8124/');

When the file is created, go to the directory its located and run:

1
node example.js

If everything is correct, you should see something like this:

1
2
$ node example.js
Server running at http://127.0.0.1:8124/

I used NodeJS and MongoDB to create a simple TinyURL application - you give it the url, it shortens it. The shortening algorithm is simple, each new url is assigned an auto-incrementing numeric index, which is then base 36 encoded to produce url key. Whenever url key is received, its base 36 decoded to get the numeric index, which is used to get the target location and send a 301 redirect header.

The application is very simple and lacks any kind of validation or error handling. The source code is open and available on github. Please feel free to fork it and use it. Feedback is much appreciated.

Comments