|
| 1 | +var express = require('express'); |
| 2 | +var redis = require('redis'); |
| 3 | +var async = require('async'); |
| 4 | + |
| 5 | +var app = express(); |
| 6 | +app.use(express.bodyParser()); |
| 7 | + |
| 8 | +var redisClient = redis.createClient(); |
| 9 | + |
| 10 | +var logger = function(req, res, next) {console.log(req.body); next();}; |
| 11 | + |
| 12 | +var auth = express.basicAuth(function(user, pass) { |
| 13 | + return pass == process.env.PASSWORD; |
| 14 | +}); |
| 15 | + |
| 16 | +app.use(logger); |
| 17 | +app.use(auth); |
| 18 | + |
| 19 | +app.get('/', function(req, res) { |
| 20 | + redisClient.lrange('ragots', 0, -1, function(err, ragotIds) { |
| 21 | + async.parallel(ragotIds.map(function(id) { |
| 22 | + return function(cb) { redisClient.get('ragots:'+id, cb); }; |
| 23 | + }), function(err, ragots) { |
| 24 | + res.render('index.jade', { ragots: ragots }); |
| 25 | + }); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +app.post('/ragots', function(req, res) { |
| 30 | + redisClient.incr('ragots:count', function(err, i) { |
| 31 | + async.parallel([ |
| 32 | + function(cb) { redisClient.set('ragots:'+i, req.body.message, cb); }, |
| 33 | + function(cb) { redisClient.lpush('ragots', i, cb); } |
| 34 | + ], function(err) { |
| 35 | + if(!err) res.redirect('/'); |
| 36 | + }); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +app.listen(3000); |
0 commit comments