Node.js & Redis App | Search user in Node.js and Redis | Register user in Redis database in Node.js | Delete user from Redis database in Node.js

Many of the applications use Redis for improving the application performance. Redis is not only used for the caching purpose. Redis supports many of the data types. Redis also support publish and subscription functionality. We can cluster the Redis database. Redis serve as in-memory, key-value data stores, although Redis is more accurately described as a data structure store.

Here in this article I'm going to use Redis with Node.js. I'll register the user in Redis database by using hmset command. I'm assuming that you have installed Redis, Node.js and NPM on you machine.

hmset command accepts multiple key value at a time and overwrites any specified fields already existing in the hash. If key does not exist, a new key holding a hash is created.
I'll require redis package to communicate with Redis. I'll also create a connection or will create the Redis client in Node.js.
So let's start the coding part. First of all I'll create the package.json which is keeping the app info and list of dependencies.

Now I'll install all the dependencies by node package manager (NPM). Run the below commend:

Now I'll be creating the entry point js file like app.js.


Here in app.js I have includes all the modules which I required. I have defined my application port as 3000 and I'm using ejs template engine for views. Later I have created a Redis client which is helping to query with Redis.

I also have used body parser here so that when I'll be sending any form data I can get the parse the request body.

For views create a directory views and put ejs code into that directory. I have used bootstrap for giving better look and feel. I'm creating partials as well like we have navigation which I wanted to keep in separate file. I also have some bootstrap CSS and JS which I wanted to use on every page so I'm keeping header and navigation in partials.




redis-node-user-listI have declared in app.js that on home route users.ejs is the home page view. Here I have a search form through which I can search the users in Redis by user id. If the user found I also list the user in table else will print the message that no user exist. You also can delete the user from Redis database by clicking on Delete button.



Here is the form to add the user. I'm keeping the user id as key in Redis hmset. I'm searching the user by user id.

Run you project with below command:

Download Code

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *