Nodejs with Redis tutorial for beginner

This tutorial for beginner nodejs developer who are trying to create a rich application for their next project. Here you’ll learn some basics of using redis server in nodejs to make your application fast and more powerful, If you didn’t listen about redis server then you must know about this awesome tool, It is just like memcached which you use to make your web application fast by storing key value pair in cached server, But redis has more feature and more powerful than memcached, You must try once.



If you don’t know how to install and configure redis server then you can read this tutoral: How to install redis server in ubuntu 14.04, 14.10, 15.04 , I assume that you have configured redis server in your machine and ready to use it in your nodejs project.

Fist create package.json file for your node project to download all the required dependencies from NPM Repository

package.json

{
  "name": "NodeJs-Redis",
  "version": "0.0.1",
  "description": "Simple Nodejs with redis tutorial",
  "dependencies": {
    "express": "^4.13.3",
    "redis": "^2.6.0-2"
  }
}

After that run sudo npm install command to install all the dependencies.

Create app.js file and write basic application architecture with redis client.

app.js

var express    = require("express");
// making connection with redis server
var redis = require("redis");
var redisClient = redis.createClient();
 
var app = express();
var port = Number(process.env.PORT || 5000);
 
console.log("Listening on "+port+", Web URL: http://localhost:"+port);
app.listen(port);

Write some error handling code on your app.js file, if there is some error on making connection with redis server then you’ll able to find the error easily.

redisClient.on("error", function (err) {
    console.log("Error " + err);
});

Setting keys and values on redis server

Now set your first key on redis server.

redisClient.set("firstKey", "Hello World!!");

Getting keys and values on redis server

Get your keys and values from redis server.

redisClient.get("firstKey", function(err, res) {
 if(err) throw err;  
 console.log(res);
});



Complete app.js file for setting and getting data from redis server.

app.js

var express    = require("express");
 
var redis = require("redis");
var redisClient = redis.createClient();
 
var app = express();
var port = Number(process.env.PORT || 5000);
 
redisClient.on("error", function (err) {
    console.log("Error " + err);
});
 
redisClient.set("firstKey", "Hello World!!");
 
redisClient.get("firstKey", function(err, res) {
 if(err) throw err;  
 console.log(res);
});
 
console.log("Listening on "+port+", Web URL: http://localhost:"+port);
app.listen(port);

After that go to your project directory and run command on terminal to see output.

cd YourProjectDirectory
node app.js

Output:
node-redis

Read official document to study more functions of redis in nodejs.
https://github.com/NodeRedis/node_redis

If you like this post please don’t forget to subscribe my public notebook for more useful stuff

Thanks 🙂