运行前必备:
执行以下命令,安装 node-redis:
npm install hiredis redis
示例代码:
var redis = require("redis");/**以下参数分别填写您的 Redis 实例内网 IP、端口号、实例 ID 和密码*/var host = "192.xx.xx.2",port = "6379",instanceid = "c53xx52f-55dc-4c22-a941-630xxx88",pwd = "12as6zb";//连接 Redisvar client = redis.createClient(port, host, {detect_buffers: true});// Redis 连接错误client.on("error", function(error) {console.log(error);});//鉴权client.auth(instanceid + ":" + pwd);/**接下来可以开始操作 Redis 实例 *///设置 Keyclient.set("redis", "tencent", function(err, reply){if (err) {console.log(err);return;}console.log("set key redis " + reply.toString() + ", value is tencent");});//获取 Keyclient.get("redis", function (err, reply) {if (err) {console.log(err);return;}console.log("get key redis is:" + reply.toString());//程序结束关闭客户端client.end();});
运行结果: