Skip to content

Commit 747f50a

Browse files
DOC-4543 split node-redis client page
1 parent e1a2693 commit 747f50a

File tree

4 files changed

+308
-226
lines changed

4 files changed

+308
-226
lines changed

content/develop/clients/nodejs.md

Lines changed: 0 additions & 226 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Connect your Node.js application to a Redis database
13+
linkTitle: node-redis (Node.js)
14+
title: node-redis guide (Node.js)
15+
weight: 4
16+
---
17+
18+
[node-redis](https://github.com/redis/node-redis) is the Redis client for Node.js.
19+
The sections below explain how to install `node-redis` and connect your application
20+
to a Redis database.
21+
22+
`node-redis` requires a running Redis or [Redis Stack]({{< relref "/operate/oss_and_stack/install/install-stack/" >}}) server. See [Getting started]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis installation instructions.
23+
24+
You can also access Redis with an object-mapping client interface. See
25+
[RedisOM for Node.js]({{< relref "/integrate/redisom-for-node-js" >}})
26+
for more information.
27+
28+
## Install
29+
30+
To install node-redis, run:
31+
32+
```bash
33+
npm install redis
34+
```
35+
36+
## Connect and test
37+
38+
Connect to localhost on port 6379.
39+
40+
```js
41+
import { createClient } from 'redis';
42+
43+
const client = createClient();
44+
45+
client.on('error', err => console.log('Redis Client Error', err));
46+
47+
await client.connect();
48+
```
49+
50+
Store and retrieve a simple string.
51+
52+
```js
53+
await client.set('key', 'value');
54+
const value = await client.get('key');
55+
```
56+
57+
Store and retrieve a map.
58+
59+
```js
60+
await client.hSet('user-session:123', {
61+
name: 'John',
62+
surname: 'Smith',
63+
company: 'Redis',
64+
age: 29
65+
})
66+
67+
let userSession = await client.hGetAll('user-session:123');
68+
console.log(JSON.stringify(userSession, null, 2));
69+
/*
70+
{
71+
"surname": "Smith",
72+
"name": "John",
73+
"company": "Redis",
74+
"age": "29"
75+
}
76+
*/
77+
```
78+
79+
To connect to a different host or port, use a connection string in the format `redis[s]://[[username][:password]@][host][:port][/db-number]`:
80+
81+
```js
82+
createClient({
83+
url: 'redis://alice:[email protected]:6380'
84+
});
85+
```
86+
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).
87+
88+
## More information
89+
90+
The [`node-redis` website](https://redis.js.org/) has more examples.
91+
The [Github repository](https://github.com/redis/node-redis) also has useful
92+
information, including a guide to the
93+
[connection configuration options](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md) you can use.
94+
95+
See also the other pages in this section for more information and examples:

0 commit comments

Comments
 (0)