我想从http://expressjs.com/guide.html尝试示例应用程序,所以我写了以下内容:
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 和以下软件包:
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