node-redis guide (JavaScript)
Connect your Node.js/JavaScript application to a Redis database
node-redis is the Redis client for Node.js/JavaScript. The sections below explain how to install node-redis and connect your application to a Redis database.
node-redis requires a running Redis server. See here for Redis Open Source installation instructions.
You can also access Redis with an object-mapping client interface. See RedisOM for Node.js for more information.
Install
To install node-redis, run:
npm install redis Connect and test
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(); await client.set('key', 'value'); const value = await client.get('key'); console.log(value); // >>> value 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" } */ await client.quit(); Store and retrieve a simple string.
import { createClient } from 'redis'; const client = createClient(); client.on('error', err => console.log('Redis Client Error', err)); await client.connect(); await client.set('key', 'value'); const value = await client.get('key'); console.log(value); // >>> value 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" } */ await client.quit(); Store and retrieve a map.
import { createClient } from 'redis'; const client = createClient(); client.on('error', err => console.log('Redis Client Error', err)); await client.connect(); await client.set('key', 'value'); const value = await client.get('key'); console.log(value); // >>> value 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" } */ await client.quit(); 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' }); To check if the client is connected and ready to send commands, use client.isReady, which returns a Boolean. client.isOpen is also available. This returns true when the client's underlying socket is open, and false when it isn't (for example, when the client is still connecting or reconnecting after a network error).
When you have finished using a connection, close it with client.quit().
import { createClient } from 'redis'; const client = createClient(); client.on('error', err => console.log('Redis Client Error', err)); await client.connect(); await client.set('key', 'value'); const value = await client.get('key'); console.log(value); // >>> value 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" } */ await client.quit(); More information
The node-redis website has more examples. The Github repository also has useful information, including a guide to the connection configuration options you can use.
See also the other pages in this section for more information and examples: