Develop NodeJS Apps

Installation

Install the nodejs driver using the following command.

  1. $ npm install redis

Working Example

Prerequisites

This tutorial assumes that you have:

  • installed YugabyteDB, created a universe, and are able to interact with it using the Redis shell. If not, please follow these steps in the quick start guide.
  • installed a recent version of node. If not, you can find install instructions here.

Writing the js code

Create a file yb-redis-helloworld.js and add the following content to it.

  1. const redis = require("redis")
  2. const client = redis.createClient();
  3. client.on("error", function (err) {
  4. console.log("Error " + err);
  5. });
  6. // Insert the user profile.
  7. const userid = 1
  8. client.hmset(userid, ["name", "John", "age", "35", "language", "NodeJS"], redis.print);
  9. // Query the user profile.
  10. client.hmget(userid, ["name", "age", "language"], redis.print);
  11. // Close the client.
  12. client.quit(function (err, res) {
  13. console.log('Exiting from quit command.');
  14. });

Running the application

To run the application, type the following:

  1. $ node yb-redis-helloworld.js

You should see the following output.

  1. Reply: OK
  2. Reply: John,35,NodeJS
  3. Exiting from quit command.