Updated on 2024-05-20 GMT+08:00

Connecting to an Instance Using Node.js

This section describes how to use Node.js to access a GeminiDB Redis instance.

Prerequisites

  • A GeminiDB Redis instance has been created and is in the Available status.
  • An ECS is available. For details, see Purchasing an ECS.
  • If the Linux operating system is used, ensure that compilation tools such as GCC have been installed on the ECS.
  • The created ECS is in the same region, AZ, VPC, and security group as the GeminiDB Redis instance.

Procedure

  1. Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.

  2. Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
  3. Run the following command to install Node.js:

    • Method 1: Run the following command to install NodeJs.

      yum install nodejs

      CentOS (Red Hat series) is used as an example. If Ubuntu (Debian series) is used, run the corresponding installation command.

    • Method 2: If the method 1 fails, use the following method to install it.

      wget https://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz --no-check-certificate ;

      tar -xvf node-v0.12.4.tar.gz;

      cd node-v0.12.4;

      ./configure;

      make;

      make install;

      CentOS (Red Hat series) is used as an example. If Ubuntu (Debian series) is used, run the corresponding installation command.

  4. After the Node.js is installed, run the following command to check the version number and ensure that the Node.js is successfully installed.

    node -v

  5. Install the JS package management tool npm.

    yum install npm

  6. Install the Node.js Redis client ioredis.

    npm install ioredis

  7. Edit the sample script for connecting to the instance.

    • Connect to a GeminiDB Redis cluster using the SDK for the single-node API on NodeJs.
      var Redis = require('ioredis');
      // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
      // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
      var pwd = process.env.EXAMPLE_PASSWORD_ENV;
      var redis = new Redis({
        port: 8635,          //Port number of the GeminiDB Redis instance obtained in step 1.
        host: '192.xx.xx.xx',   //Enter the load balancer IP address obtained in step 1.
        family: 4,           //4 indicates IPv4, and the 6 indicates IPv6.
        password: pwd,
        db: 0
      });
      redis.set('key', 'Nodejs tst ok!');
      redis.get('key', function (err, result) {
        console.log(result);
      });
    • Connect to the GeminiDB Redis cluster using the SDK for the cluster API on Node.js.
      const Redis = require("ioredis");
      // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
      // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
      var pwd = process.env.EXAMPLE_PASSWORD_ENV;
      const cluster = new Redis.Cluster([
         {
           port: 8635,          //Port number of the GeminiDB Redis instance obtained in step 1.
        host: '192.xx.xx.xx',   //Enter the load balancer IP address obtained in step 1.
           family: 4,           // 4 indicates IPv4, and the 6 indicates IPv6.
           password: pwd,
           db: 0
         },
       ]);
       
       cluster.set("foo", "nodejs is ok!");
       cluster.get("foo", (err, res) => {
         console.log(res);
       });

  8. Run the sample script and verify that the result is normal.

    node ioredisdemo.js