“Next article” micro-analytics: Redis and Node

My last post demonstrated a micro-analytics app using Redis and Ruby/Sinatra. Here is the equivalent using Node/Express/Node Redis.

var express = require('express');
var app = express();
var db = require('redis-url').connect(process.env.REDISTOGO_URL);

/**
 * Log navigation from one location to the next
 * @param {string} from: one location
 * @param {string} to: the next location
 * @returns {number} How many times this from/to pair has been logged
 */
app.get('/log', function(request, response) {
	var args = [request.query.from, 1, request.query.to];
	var reply = db.zincrby(args, function(err, count) {
		// How to handle err?
	    response.send(count);
	});
});

/**
 * Get the most popular "next" location
 * @param {string} from: location
 * @returns {string} the most popular "next" location
 */
app.get('/get', function(request, response) {
	var args = [request.query.from, 0, 0];
	db.zrevrange(args, function(err, reply) {
		if (err) {
			throw err;
		} else {
	    	response.send(reply);
	    }
	});
});

var myPort = process.env.PORT || 3000;
app.listen(myPort);
console.log('Listening on port ' + myPort);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s