Home  >  Article  >  Database  >  How Node.js operates redis to add query function

How Node.js operates redis to add query function

WBOY
WBOYforward
2023-05-26 16:26:301214browse

An example

The installation of the redis server will not be introduced here, the focus is not here. Those who are interested can install it themselves.

For a new module, we need to install the redis module in our project before it can be used.

Command

cnpm install redis

How Node.js operates redis to add query function

Create a new redis.js file with the following code:

//引入redis
var 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);

    });
  };
});

Inquire

How Node.js operates redis to add query function

The above is the detailed content of How Node.js operates redis to add query function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete