简体   繁体   中英

Node.js with simple redis application throwing 'unhandled error'

I wanted to try out example application from http://expressjs.com/guide.html so I wrote the following :

var express = require('express')
var redis = require('redis')
var db = redis.createClient();
var app = express();

app.use(function(req, res, next){
  var ua = req.headers['user-agent'];
  db.zadd('online', Date.now(), ua, next);
});

app.use(function(req, res, next){
  var min = 60 * 1000;
  var ago = Date.now() - min;
  db.zrevrangebyscore('online', '+inf', ago, function(err, users){
    if (err) return next(err);
    req.online = users;
    next();
  });
});

app.get('/', function(req,res){
  res.send(req.online.length + ' users online');
});


app.listen(3000);

but after trying to launch it I get

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
    at RedisClient.on_error (/home/USER/programming/nodejs/express1/node_modules/redis/index.js:148:24)
    at Socket.<anonymous> (/home/USER/programming/nodejs/express1/node_modules/redis/index.js:83:14)
    at Socket.EventEmitter.emit (events.js:96:17)
    at Socket._destroy.self.errorEmitted (net.js:329:14)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

I use node.js 0.8.18 and following packages:

app@0.0.1 /home/USER/programming/nodejs/express1
├─┬ express@3.1.0
│ ├── buffer-crc32@0.1.1
│ ├── commander@0.6.1
│ ├─┬ connect@2.7.2
│ │ ├── bytes@0.1.0
│ │ ├── formidable@1.0.11
│ │ ├── pause@0.0.1
│ │ └── qs@0.5.1
│ ├── cookie@0.0.5
│ ├── cookie-signature@0.0.1
│ ├── debug@0.7.2
│ ├── fresh@0.1.0
│ ├── methods@0.0.1
│ ├── mkdirp@0.3.3
│ ├── range-parser@0.0.4
│ └─┬ send@0.1.0
│   └── mime@1.2.6
└── redis@0.8.2

Check the port of reddis. If correct, give a wildcard to that port in any firewall which might block it. Check whether something else is using that port. Check your connection settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM