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
Ruben Bridgewater f1a7bcd735 chore: use standard
2017-05-06 07:06:52 +02:00

37 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) {
if (err) throw err
redisInfo = reply // stash response in outer scope
})
redisClient.incr('requests', function (err, reply) {
if (err) throw err
totalRequests = reply // stash response in outer scope
})
redisClient.hincrby('ip', request.connection.remoteAddress, 1)
redisClient.hgetall('ip', function (err, reply) {
if (err) throw err
// 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)