Node.js 安装 Redis:完整指南
在 Node.js 中安装 Redis 可以使用 npm 来完成,以下步骤将带您逐步进行:
- 打开终端并安装 Redis 模块:
npm install redis
- 在 Node.js 应用中引入 Redis 模块:
const redis = require('redis');
- 连接 Redis 服务器:
const client = redis.createClient();
- 测试 Redis 连接:
client.on('connect', function() {
console.log('Redis 连接成功!');
});
client.on('error', function() {
console.log('Redis 连接失败!');
});
- 使用 Redis:
// 设置键值
client.set('hello', 'world', redis.print);
// 获取键值
client.get('hello', function(err, reply) {
console.log(reply);
});
以上步骤详细说明了如何在 Node.js 中安装和使用 Redis。
原文地址: https://www.cveoy.top/t/topic/mvew 著作权归作者所有。请勿转载和采集!