Install Redis Client for Node.
js
1) Install the redis package:
CMD:
npm install redis
2) Create a Redis Connection in Node.js
Now, you can create a Redis connection in your Node.js code. Here’s how:
a) Create a new file, app.js:
b) Add the following code to connect to Redis, set, and get a value:
const redis = require('redis');
// Create a Redis client
const client = redis.createClient();
// Listen for connection events
client.on('connect', function() {
console.log('Connected to Redis');
});
client.on('error', function (err) {
console.log('Redis connection error: ' + err);
});
// Set a key-value pair in Redis
client.set('key', 'value', (err, reply) => {
if (err) {
console.log('Error setting value:', err);
} else {
console.log('Set key response:', reply);
}
// Get the value back
client.get('key', (err, reply) => {
if (err) {
console.log('Error getting value:', err);
} else {
console.log('Value:', reply);
}
client.quit(); // Close the Redis connection
});
});
3) Run the Node.js app:
node app.js
If everything is set up correctly, you should see output like:
Connected to Redis
Set key response: OK
Value: value
4) Connect to Redis with Authentication
If you’ve enabled authentication on your Redis server (requirepass in
redis.conf), you will need to pass the password when creating the Redis client.
Here’s how to connect with a password:
const redis = require('redis');
// Create a Redis client with authentication
const client = redis.createClient({
password: 'your_secure_password'
});
client.on('connect', function() {
console.log('Connected to Redis');
});
client.on('error', function (err) {
console.log('Redis connection error: ' + err);
});
client.set('key', 'value', (err, reply) => {
if (err) {
console.log('Error setting value:', err);
} else {
console.log('Set key response:', reply);
}
client.get('key', (err, reply) => {
if (err) {
console.log('Error getting value:', err);
} else {
console.log('Value:', reply);
}
client.quit(); // Close the Redis connection
});
});
5) Use Promises with Redis in Node.js
You can also use promises with Redis by using the async/await syntax. For
this, you’ll need to use promisify from the util module, since the redis library’s
API is callback-based.
Here’s how to do it:
const redis = require('redis');
const { promisify } = require('util');
// Create a Redis client
const client = redis.createClient();
// Promisify the Redis GET and SET methods
const getAsync = promisify(client.get).bind(client);
const setAsync = promisify(client.set).bind(client);
async function redisOperations() {
try {
await setAsync('key', 'value');
console.log('Key set successfully');
const value = await getAsync('key');
console.log('Retrieved value:', value);
} catch (err) {
console.error('Error:', err);
} finally {
client.quit(); // Close the Redis connection
}
}
redisOperations();
This approach lets you handle Redis operations using promises and async/await,
making your code cleaner and easier to manage.
Summary:
Install Redis client: npm install redis
Connect to Redis: Use redis.createClient() in your Node.js code.
Perform operations: Use Redis commands like set, get, etc.
Promisify the API: Optionally use promisify for async/await support.