1 // A simple web server that generates dyanmic content based on responses from Redis
3 var http = require("http"), server,
4 redis_client = require("redis").createClient();
6 server = http.createServer(function (request, response) {
7 response.writeHead(200, {
8 "Content-Type": "text/plain"
11 var redis_info, total_requests;
13 redis_client.info(function (err, reply) {
14 redis_info = reply; // stash response in outer scope
16 redis_client.incr("requests", function (err, reply) {
17 total_requests = reply; // stash response in outer scope
19 redis_client.hincrby("ip", request.connection.remoteAddress, 1);
20 redis_client.hgetall("ip", function (err, reply) {
21 // This is the last reply, so all of the previous replies must have completed already
22 response.write("This page was generated after talking to redis.\n\n" +
23 "Redis info:\n" + redis_info + "\n" +
24 "Total requests: " + total_requests + "\n\n" +
26 Object.keys(reply).forEach(function (ip) {
27 response.write(" " + ip + ": " + reply[ip] + "\n");