Redis : Starter Setup, Learn Basics for Beginners (Bite-size Article)
koshirok096
Posted on October 28, 2023
Introduction
This is a continuation of the previous article. Since the previous article was only a basic overview, this article will take you step by step through the initial setup of Redis for web development, in the Express environment.
This article is aimed at beginners who have just started learning Redis, so it will be short and basic.
Install and Setup
First of all, we need to install the redis package for use in our project.
To use Redis in your Express app, run the following command in your project directory to install the redis package:
npm install redis
Running this code will install the Redis client for Node.js, called Node-Redis, into your project.
Connect Redis and Express
To use Redis in your project, connect to the Redis server as follows:
const express = require('express');
const redis = require('redis'); // Redis module is loaded
const app = express();
// Create Redis Client
const client = redis.createClient();
// Error Handling
client.on('error', function (err) {
console.log('Redis Error: ' + err);
});
app.listen(3000, function() {
console.log('Server is running on port 3000');
});
The line const client = redis.createClient();
creates Redis client to connect Redis server, and the code block client.on('error', function (err) {...});
is for error handling.
Tip: What is createClient?
createClient
is a function provided by the Redis module of Node.js. It is used to establish a connection with the Redis server.
It is a method for creating a Redis client and creates an instance to communicate with the Redis server.
Code Examples
Up to this point, Redis has been successfully set up in your project.
Let's build upon the current code to add the functionality to store data in Redis and retrieve data from Redis.
const express = require('express');
const redis = require('redis'); // Redis module is loaded
const app = express();
// Create Redis Client
const client = redis.createClient();
// Error Handling
client.on('error', function (err) {
console.log('Redis Error: ' + err);
});
// 1. Store Data in Redis
app.get('/set', function(req, res) {
client.set('key', 'value', function(err, reply) {
if (err) {
console.error(err);
res.status(500).send('error');
} else {
res.send('Stored data in Redis');
}
});
});
// 2. Retrieve Data from Redis
app.get('/get', function(req, res) {
client.get('key', function(err, reply) {
if (err) {
console.error(err);
res.status(500).send('error');
} else {
res.send('Data from Redis: ' + reply);
}
});
});
app.listen(3000, function() {
console.log('Server is running on port 3000');
});
1. Store Data in Redis
The code block app.get('/set', function(req, res) { ... }
defines the '/set'
endpoint to store data in Redis.
The code block client.set('key', 'value', function(err, reply) { ... }
saves a key-value pair ('key' as the key and 'value' as the value) in Redis using the client.set
function.
This is an asynchronous operation, and the result is checked in the callback function.
2. Retrieve Data from Redis
The code block app.get('/get', function(req, res) { ... }
defines the '/get'
endpoint to retrieve data from Redis.
The code block client.get('key', function(err, reply) { ... }
retrieves the value associated with the 'key' from Redis using the client.get
function.
This is also an asynchronous operation, and the data is processed in the callback function.
Tip: Location of Data
The data in this code is stored in the Redis server. Assuming the Express application and the Redis server are running on the same machine or on different machines within the same network, the data is associated with a specific 'key' within the Redis server.
In essence, the stored data resides within the Redis server, and the data retrieved through a GET
request is also obtained from a specific 'key' within the same Redis server.
Conclusion
In this article, I covered the initial setup and provided code examples for setting and retrieving data using the 'Set' and 'Get' operations.
I intended to include more information, but due to time constraints today, I plan to consolidate that content in forthcoming articles.
Thank you for reading :)
Posted on October 28, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.