2013-02-10 79 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

相關問題