1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00
Files
node-redis/examples/web_server.js
2017-05-06 01:49:05 +02:00

34 lines
1.2 KiB
JavaScript

'use strict';
// A simple web server that generates dyanmic content based on responses from Redis
var http = require('http');
var redisClient = require('redis').createClient();
http.createServer(function (request, response) { // The server
response.writeHead(200, {
'Content-Type': 'text/plain'
});
var redisInfo, totalRequests;
redisClient.info(function (err, reply) {
redisInfo = reply; // stash response in outer scope
});
redisClient.incr('requests', function (err, reply) {
totalRequests = reply; // stash response in outer scope
});
redisClient.hincrby('ip', request.connection.remoteAddress, 1);
redisClient.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' + redisInfo + '\n' +
'Total requests: ' + totalRequests + '\n\n' +
'IP count: \n');
Object.keys(reply).forEach(function (ip) {
response.write(' ' + ip + ': ' + reply[ip] + '\n');
});
response.end();
});
}).listen(80);