Skip to content

Commit

Permalink
Simple web server that integrates with Redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
mranney committed Oct 2, 2010
1 parent e31e0c6 commit b24edbe
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/web_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// A simple web server that generates dyanmic content based on responses from Redis

var http = require("http"), server,
redis_client = require("redis").createClient();

server = http.createServer(function (request, response) {
response.writeHead(200, {
"Content-Type": "text/plain"
});

var redis_info, total_requests;

redis_client.info(function (err, reply) {
redis_info = reply; // stash response in outer scope
});
redis_client.incr("requests", function (err, reply) {
total_requests = reply; // stash response in outer scope
});
redis_client.hincrby("ip", request.connection.remoteAddress, 1);
redis_client.hgetall("ip", function (err, reply) {
// This is the last reply, so all of the previous replies must have completed already
response.write("This page was generated after talking to redis.\n\n" +
"Redis info:\n" + redis_info + "\n" +
"Total requests: " + total_requests + "\n\n" +
"IP count: \n");
Object.keys(reply).forEach(function (ip) {
response.write(" " + ip + ": " + reply[ip] + "\n");
});
response.end();
});
}).listen(80);

0 comments on commit b24edbe

Please sign in to comment.