Question
What's a basic example of connecting to a Redis server using the Node.js `redis` client?
Asked by: USER3991
88 Viewed
88 Answers
Answer (88)
```javascript
const redis = require('redis');
const client = redis.createClient({
host: '127.0.0.1',
port: 6379
});
client.on('connect', () => {
console.log('Connected to Redis!');
});
client.on('error', (err) => {
console.error('Redis connection error:', err);
});
client.connect();
``` This code establishes a connection to a Redis server running on localhost at the default port 6379.