2013-02-10 61 views
0

我想从http://expressjs.com/guide.html尝试示例应用程序,所以我写了下面的:的Node.js简单的Redis应用抛出“未处理的错误”

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); 

而是试图启动它后,我得到

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) 

我用node.js 0.8.18及以下软件包:

[email protected] /home/USER/programming/nodejs/express1 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│ └── [email protected] 
└── [email protected] 
+0

您的本地Redis安装是否正确配置并在端口6379上运行? 'ECONNREFUSED'表示问题出在Redis上,或者是系统端口设置/防火墙,而不是Node.js。 – cjohn 2013-02-10 15:17:04

回答

0

检查reddis港。如果正确,请在任何可能阻止该端口的防火墙中为该端口设置通配符。检查是否有其他人正在使用该端口。检查您的连接设置。

+0

嗯,这是令人尴尬的,但我......没有运行redis ......非常感谢:) – Patryk 2013-02-10 16:03:59

+1

有时候我会问我不知道答案的其他人。如果你问自己正确的问题,那么你可以帮助你自己提供答案。 – 2013-02-10 16:20:12