Skip to content

Commit 882c7f2

Browse files
author
Simon Prickett
committed
Updated for Node Redis 4.
1 parent 6583825 commit 882c7f2

File tree

1 file changed

+81
-60
lines changed

1 file changed

+81
-60
lines changed
Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,129 @@
11
---
22
id: index-usingnodejs
3-
title: How to cache JSON data in Redis with NodeJS
4-
sidebar_label: RedisJSON and NodeJS
3+
title: How to cache JSON data in Redis with Node.js
4+
sidebar_label: RedisJSON and Node.js
55
slug: /howtos/redisjson/using-nodejs
6-
authors: [ajeet]
6+
authors: [ajeet,simon]
77
---
88

9-
Node.js has become incredibly popular for both web and mobile application development. It is a runtime for JavaScript so it can run JavaScript code on the machine. Node.js can be installed on MacOS, Linux and Windows system. The Node Package Manager (npm) enables developers to reuse useful codes which are tried and tested and helps you to build strong and steady applications quickly.
9+
Node.js has become incredibly popular for both web and mobile application development. Node.js can be installed on MacOS, Linux and Windows systems. The Node Package Manager (npm) enables developers to install packages which are tried and tested libraries that help you to build applications quickly.
1010

11-
Node.js is a fast web framework, but adding the power, speed and flexibility of Redis can take it to the next level.Redis is best suited to situations that require data to be retrieved and delivered to the client as quickly as possible
12-
If you’re using Node, you can use the node-redis module to interact with Redis. If you’re using RediJSON, you can use redis-rejson module to interact with RedisJSON.
11+
Node.js is a fast runtime, but adding the power, speed and flexibility of Redis can take it to the next level. Redis is best suited to situations that require data to be retrieved and delivered to the client as quickly as possible.
1312

14-
The 'RedisJSON Module plugin for node_redis' package allows node_redis (2.8+) to interface with the Redis module RedisJSON.To use this module, you will need Redis 4.0 or higher and the redisjson module installed.
13+
[RedisJSON](https://redisjson.io) is an add-on module that adds JSON as a native data type to Redis. It enables atomic, in place operations to be performed on JSON documents stored in Redis.
1514

16-
Follow the below steps to get started with RedisJSON and Node.js:
15+
We'll use the [node-redis](https://npmjs.com/package/redis) client to connect to Redis and leverage the power of RedisJSON.
1716

18-
### Step 1. Run RedisMod Docker container
19-
20-
This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis Lab.
17+
### Step 1. Run the redismod Docker Container
2118

19+
This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis, Inc.
2220

2321
```bash
24-
docker run -d -p 6379:6379 redislabs/redismod:latest
22+
$ docker run -d -p 6379:6379 redislabs/redismod:latest
2523
```
2624

25+
### Step 2. Install Node.js
2726

28-
### Step 2. Install Node
27+
Download and install the current LTS (Long Term Support) version of Node.js from the [nodejs.org](https://nodejs.org/) website.
2928

30-
```bash
31-
brew install node
32-
```
29+
### Step 3. Initialize an npm Project
3330

34-
### Step 3. Install Redis
31+
Run `npm init` to initialize a new project. Use the default answers to all the questions:
3532

36-
Node Redis is a high performance Node.js Redis client.
33+
```
34+
$ mkdir jsondemo
35+
$ cd jsondemo
36+
$ npm init
37+
```
3738

38-
```bash
39-
npm install redis
39+
Now edit `package.json` and add the line `"type": "module"`. The file should look something like this:
40+
41+
```json
42+
{
43+
"name": "jsondemo",
44+
"type": "module",
45+
"version": "1.0.0",
46+
"description": "",
47+
"main": "index.js",
48+
"scripts": {
49+
"test": "echo \"Error: no test specified\" && exit 1"
50+
},
51+
"author": "",
52+
"license": "ISC"
53+
}
4054
```
4155

42-
### Step 4. Install RedisJSON module
56+
### Step 4. Install node-redis
57+
58+
[node-redis](https://npmjs.com/package/redis) is a high performance Node.js Redis client with support for the RedisJSON module. Install it using `npm`:
4359

4460
```bash
45-
npm -i redis-rejson
61+
$ npm install redis
4662
```
4763

48-
### Step 5. Create a file
64+
### Step 5. Create a JavaScript File
4965

50-
Copy the below content and save the file as “app.js
66+
Copy the code below into a file called `app.js`:
5167

68+
```javascript
69+
import { createClient } from 'redis';
5270

53-
```bash
54-
const redis=require("redis");
55-
rejson = require('redis-rejson');
56-
57-
rejson(redis); /* important - this must come BEFORE creating the client */
58-
let client= redis.createClient({
59-
port:6379,
60-
host:'localhost'
61-
});
62-
63-
let test_node_key = 'test_node';
64-
client.json_set(test_node_key, '.', '{"node":4303}', function (err) {
65-
if (err) { throw err; }
66-
console.log('Set JSON at key ' + test_node_key + '.');
67-
client.json_get(test_node_key, '.node', function (err, value) {
68-
if (err) { throw err; }
69-
console.log('value of node:', value); //outputs 4303
70-
client.quit();
71-
});
72-
});
73-
```
71+
async function redisJSONDemo () {
72+
try {
73+
const TEST_KEY = 'test_node';
7474

75+
const client = createClient();
76+
await client.connect();
7577

78+
// RedisJSON uses JSON Path syntax. '.' is the root.
79+
await client.json.set(TEST_KEY, '.', { node: 4303 });
80+
const value = await client.json.get(TEST_KEY, {
81+
// JSON Path: .node = the element called 'node' at root level.
82+
path: '.node'
83+
});
7684

85+
console.log(`value of node: ${value}`);
7786

78-
### Step 6. Run the app
87+
await client.quit();
88+
} catch (e) {
89+
console.error(e);
90+
}
91+
}
7992

80-
```bash
81-
node app.js
93+
redisJSONDemo();
8294
```
8395

96+
### Step 5. Run the Application
97+
98+
Start the application as follows:
8499

85100
```bash
86-
Set JSON at key test_node.
87-
value of node: 4303
101+
$ node app.js
88102
```
89103

104+
You should see this output:
105+
106+
```bash
107+
value of node: 4303
90108
```
91-
% redis-cli
92-
127.0.0.1:6379> monitor
93-
OK
94109

95-
1628071593.564178 [0 172.17.0.1:65054] "info"
96-
1628071593.567058 [0 172.17.0.1:65054] "json.set" "test_node" "." "{\"node\":4303}"
97-
1628071593.572035 [0 172.17.0.1:65054] "json.get" "test_node" ".node"
110+
Using the Redis [`MONITOR`](https://redis.io/commands/monitor) command, you can see the Redis commands that node-redis sent to the Redis server while running the application:
111+
98112
```
113+
$ redis-cli
114+
127.0.0.1:6379> monitor
115+
OK
99116
117+
1637866932.281949 [0 127.0.0.1:61925] "JSON.SET" "test_node" "." "{\"node\":4303}"
118+
1637866932.282842 [0 127.0.0.1:61925] "JSON.GET" "test_node" ".node"
119+
```
100120

101121
### References
102122

123+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
103124
- [RedisJSON and Python](/howtos/redisjson/using-python)
104-
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
105-
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)
106-
- Learn more about [RedisJSON](https://oss.redislabs.com/redisjson/) in the Quickstart tutorial.
107-
- [How to build shopping cart app using NodeJS and RedisJSON](/howtos/shoppingcart)
125+
- [How to store and retrieve nested JSON documents](/howtos/redisjson/storing-complex-json-document)
126+
- [Importing JSON data into Redis using Node.js](/howtos/redisjson/using-nodejs)
127+
- Learn more about [RedisJSON](https://redisjson.io/) in the Quickstart tutorial.
128+
- [How to build a shopping cart app using Node.js and RedisJSON](/howtos/shoppingcart)
108129
- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redislabs.com/blog/index-and-query-json-docs-with-redis/)

0 commit comments

Comments
 (0)