What's a basic example of connecting to a Redis server using the Node.js `redis` client?

Responsive Ad Header

Question

Grade: Education Subject: Support
What's a basic example of connecting to a Redis server using the Node.js `redis` client?
Asked by:
88 Viewed 88 Answers

Answer (88)

Best Answer
(399)
```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.