我正在使用redis来存储项目中的会话。
在
每次都连接到redis,我该如何在
在
app.js
中:var client = redis.createClient();
client.on('error', function (err) {
console.log('could not establish a connection with redis. ' + err);
});
client.on('connect', function (err) {
console.log('connected to redis successfully');
});
在我的routes.js
文件中,我需要从redis获取所有信息,因此我使用以下代码:
var client = redis.createClient();
client.on('error', function(err){
console.log('Something went wrong ', err)
});
每次都连接到redis,我该如何在
app.js
中连接一次redis并在任何地方使用它?请提出建议。谢谢。app.js
module.exports.client = client;
routes.js
var client = require("../app.js").client;
client.hgetall();
但是我收到了错误
TypeError: Cannot read property 'hgetall' of undefined
app.js
中的client
导出,然后在routes.js
中导入该client
。 - Ritwick Deyredis.createClient();
一个实例/请求,还是一个实例/用户,或者是应用程序全生命周期内的一个实例。 - Ritwick Dey