“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);

Leave a Reply Cancel reply

Exit mobile version
%%footer%%