Nodejs-ioredis

Redis with Node.js (ioredis)

In order to use Redis with Node.js, you need to install a Node.js Redis client. The following sections demonstrate the use of ioredis, a community-recommended Redis client for Node.js with build-in support for promises.

Another community-recommended client for Node.js developers is node_redis. Additional Node.js clients for Redis can be found under the Node.js section of the Redis Clients page.

Install ioredis

See the ioredis README file for installation instructions.

To install ioredis, run:


npm install ioredis

Connect to Redis

This example code creates a connection to Redis:


const Redis = require('ioredis');
const redis = new Redis({
    host: '<hostname>',
    port: <port>,
    password: '<password>'
});
Replace the values in the example with the values for your Redis instance:

- The name of the host your database runs on - The port that the database is running on (default: 6379) - The default Redis password, if configured Note: Remember to always store passwords outside of your code, for example in environment variables.

TLS

This example shows how to configure ioredis to make a connection to Redis using TLS:


const Redis = require('ioredis');
const fs = require('fs');

const redis = new Redis({
    host: 'hostname',
    port: <port>,
    tls: {
        key: fs.readFileSync('path_to_keyfile', 'ascii'),
        cert: fs.readFileSync('path_to_certfile', 'ascii'),
        ca: [ fs.readFileSync('path_to_ca_certfile', 'ascii') ]
    }
});
Where you must provide:

- The name of the host your database runs on - The port that the database is running on (default: 6379)

My Test

const Redis = require('ioredis');
const fs = require('fs');

const tlsSettings = {
  rejectUnauthorized: false,
  key: fs.readFileSync('/opt/Will_Project/formdataAPI/certificate/ys_client.key', 'ascii'),
  cert: fs.readFileSync('/opt/Will_Project/formdataAPI/certificate/ys_client.crt', 'ascii'),
  ca: [fs.readFileSync('/opt/Will_Project/formdataAPI/certificate/ys_ca.key', 'ascii')]
};

const redis = new Redis({
  host: '127.0.0.1',
  port: 6379,
  tls: tlsSettings,
});

const setKeyToRedis = (async () => {
  await selectDB(1);
  await setKey('ASX8888:inLane',{inTime:"2022-03-06T12:11:33"});
})()

function selectDB(dbNumber){
  return new Promise((resolve, reject) => {
    redis.select(dbNumber, () => {
      console.log(new Date(), 'Redis Client Connect To DB',dbNumber);
      resolve()
    })
  });
}

function setKey(key, data ){
  return new Promise((resolve, reject) => {
    redis.set(key, JSON.stringify(data), function(err, result) {
      if (err) reject(err)
      console.log(new Date(), 'Set Key',key,'Result ->' + result);
      resolve(result);
    })
  })
}
willhsu@ubuntu20:/opt/Will_Project/formdataAPI$ node test.js
2022-03-07T04:03:15.390Z Redis Client Connect To DB 1
2022-03-07T04:03:15.392Z Set Key ASX8888:inLane Result ->OK
willhsu@ubuntu20:/opt/Will_Project/formdataAPI/certificate$ redis-cli --tls --cert ys_client.crt --key ys_client.key --cacert ys_ca.crt
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "ASX8888:inLane"
127.0.0.1:6379[1]> get ASX8888:inLane
"{\"inTime\":\"2022-03-06T12:11:33\"}"
127.0.0.1:6379[1]>