关于redis服务端的安装这里不再介绍,重点不在这里。感兴趣的可以自行安装。
对于一个新的模块,我们需要在我们的项目中先安装redis模块才能用。
命令
cnpm install redis
新建一个redis.js的文件,代码如下:
//引入redisvar redis = require(redis);//创建redis客户端var client = redis.createclient(6379, 127.0.0.1);//连接错误处理client.on(error, function (error) { console.log(error);});//redis验证 (如果redis没有开启验证,此配置可以不写)client.auth(123456);//查找client.select(15, function (error) { if (error) { console.log(error); } else { client.set(node_redis_key, json.stringify({ name: wolfy, age: 28 }), function (error, res) { if (error) { console.log(error); } else { console.log(res); }; //操作完成,关闭redis连接 client.end(true); }); };});
查询
以上就是node.js如何操作redis实现添加查询功能的详细内容。
