Node-redis
node-redis is a modern, high-performance Redis client for Node.js. node-redis requires a running Redis or Redis Stack server. See Getting started for Redis installation instructions.
Usage
To install node-redis, run:
Connect to localhost on port 6379.
import { createClient } from 'redis';
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
Store and retrieve a simple string.
Store and retrieve a map.
await client.hSet('user-session:123', {
name: 'John',
surname: 'Smith',
company: 'Redis',
age: 29
})
let userSession = await client.hGetAll('user-session:123');
console.log(JSON.stringify(userSession, null, 2));
/*
{
"surname": "Smith",
"name": "John",
"company": "Redis",
"age": "29"
}
*/
To connect to a different host or port, use a connection string in the format redis[s]://[[username][:password]@][host][:port][/db-number]
:
createClient({
url: 'redis://alice:[email protected]:6380'
});