NodeJS and Redis Example

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re developing an application using NodeJS, you may want to harness the power of a Redis data store for your app. Fortunately, it’s easy to integrate Redis into your NodeJS apps. In this article, we’ll look at a NodeJS and Redis example and see how a Redis key-value store can be used within the NodeJS framework.

Prerequisites

Before attempting to follow along with our NodeJS and Redis example, please make sure the following prerequisites are in place:

  • Redis must be installed on your device. To verify that it’s installed, you can use the redis server --version or redis cli --version commands to see what version number you’re using.

  • You should have some basic knowledge of how to use both NodeJS and the Redis data store.

Install Node.JS and NPM

If you’re running Linux, you can add the latest release of NodeJS by using the PPA in your system:

1
sudo apt-get install curl python-software-properties

Then install NodeJS using apt-get:

1
sudo apt-get install nodejs

After installing, you can check the version number with this command:

1
node -v

You can also check the version number of npm:

1
npm -v

Create a directory for the Node app

If you’re using a machine running the UNIX operating system, you can use the terminal to create a directory for the Node application:

To create a directory, use the bash command in the terminal:

1
mkdir redis_node

You can then go to the directory using the following command:

1
cd redis_node

Next, you’ll need to create a package using npm, which is a tool used for the consistent runtime environment in NodeJS. This will create the package.json file with the command:

1
npm init -y

NOTE: The -y option after the npm command indicates that you will accept the default credentials set by the package manager.

At this point, you’ll need to open up the project directory and create a file to use for our NodeJS Redis example. Open the code editor of your choice, such as VSCODE or Sublime.

Install the Node dependencies

Our next step will be to install any Node dependencies. In this tutorial, we’ll be installing the redis module for NodeJs:

1
npm install redis

Start the Node app

Now that we have installed the Redis module, let’s create a file that will define how Redis is connected to NodeJS:

app.js

1
2
3
4
5
6
var redis = require("redis");
var client = redis.createClient();

client.on("connect", function() {
  console.log("You are now connected");
});

This will establish a connection to the Redis data store; by default, the port is 6397 and the host is 127.0.0.1.

Store a string in Redis

Redis stores data in a key-value format. It supports a number of data types that are used in various functions of the client object. For example:

1
client.set("student", "Laylaa");

The code shown above will store the string value Laylaa in the student key that is set within the function.

In the next example, we pass the function to produce a callback which will receive notification when the operation is complete:

1
2
3
client.set("student", "Laylaa", function(err, reply) {
  console.log(reply);
});

The result would then look like the following:

1
2
client.get('student', function(err, reply) {
    console.log(reply);

NOTE: The object client.get(); will display the key that is stored in the Redis database. It will show in the console due to the callback function that is set in the argument reply. If a value for student isn’t set, it will not display the message.

Here’s what the current output of our code will look like:

1
2
3
4
linux@linux-NECq:~/redis_node$ node app
You are now connected
OK
Laylaa

Store objects in Redis

If you want to make your job even easier, you can use a different Redis data types– a hash. Here’s an example of a typical hash:

1
client.hmset("employees", { HR: "Anthony", MIS: " Clint", Accounting: "Mark" });

The following code will display and retrieve the stored object in Redis using NodeJS:

1
2
3
client.hgetall("employees", function(err, object) {
  console.log(object);
});

The result will look like the following:

1
2
You are now connected
{ HR: 'Anthony', MIS: ' Clint', Accounting: 'Mark' }

Store lists in Redis

If you’re planning to store lists in Redis using NodeJS, you can use the RPUSH command in the client object function:

1
2
3
client.rpush(["vegetable", "carrot", "celery"], function(err, reply) {
  console.log(reply);
});

To display a stored list in Redis, use the command shown below:

1
2
3
client.lrange("vegetable", 0, -1, function(err, reply) {
  console.log(reply);
});

The result will look like:

1
2
3
You are now connected
2
[ 'carrot', 'celery' ]

Conclusion

If you’re looking for a fast and powerful data store to use with your NodeJS applications, Redis is a natural choice. In this article, we looked at a NodeJS and Redis example that showed how these two technologies can be used together. With our example to use as a guide, you’ll be able to add Redis functionality to your own NodeJS applications.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.