|
1 | 1 | --- |
2 | 2 | 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 |
5 | 5 | slug: /howtos/redisjson/using-nodejs |
6 | | -authors: [ajeet] |
| 6 | +authors: [ajeet,simon] |
7 | 7 | --- |
8 | 8 |
|
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. |
10 | 10 |
|
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. |
13 | 12 |
|
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. |
15 | 14 |
|
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. |
17 | 16 |
|
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 |
21 | 18 |
|
| 19 | +This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis, Inc. |
22 | 20 |
|
23 | 21 | ```bash |
24 | | - docker run -d -p 6379:6379 redislabs/redismod:latest |
| 22 | +$ docker run -d -p 6379:6379 redislabs/redismod:latest |
25 | 23 | ``` |
26 | 24 |
|
| 25 | +### Step 2. Install Node.js |
27 | 26 |
|
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. |
29 | 28 |
|
30 | | -```bash |
31 | | - brew install node |
32 | | -``` |
| 29 | +### Step 3. Initialize an npm Project |
33 | 30 |
|
34 | | -### Step 3. Install Redis |
| 31 | +Run `npm init` to initialize a new project. Use the default answers to all the questions: |
35 | 32 |
|
36 | | -Node Redis is a high performance Node.js Redis client. |
| 33 | +``` |
| 34 | +$ mkdir jsondemo |
| 35 | +$ cd jsondemo |
| 36 | +$ npm init |
| 37 | +``` |
37 | 38 |
|
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 | +} |
40 | 54 | ``` |
41 | 55 |
|
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`: |
43 | 59 |
|
44 | 60 | ```bash |
45 | | - npm -i redis-rejson |
| 61 | +$ npm install redis |
46 | 62 | ``` |
47 | 63 |
|
48 | | -### Step 5. Create a file |
| 64 | +### Step 5. Create a JavaScript File |
49 | 65 |
|
50 | | -Copy the below content and save the file as “app.js” |
| 66 | +Copy the code below into a file called `app.js`: |
51 | 67 |
|
| 68 | +```javascript |
| 69 | +import { createClient } from 'redis'; |
52 | 70 |
|
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'; |
74 | 74 |
|
| 75 | + const client = createClient(); |
| 76 | + await client.connect(); |
75 | 77 |
|
| 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 | + }); |
76 | 84 |
|
| 85 | + console.log(`value of node: ${value}`); |
77 | 86 |
|
78 | | -### Step 6. Run the app |
| 87 | + await client.quit(); |
| 88 | + } catch (e) { |
| 89 | + console.error(e); |
| 90 | + } |
| 91 | +} |
79 | 92 |
|
80 | | -```bash |
81 | | - node app.js |
| 93 | +redisJSONDemo(); |
82 | 94 | ``` |
83 | 95 |
|
| 96 | +### Step 5. Run the Application |
| 97 | + |
| 98 | +Start the application as follows: |
84 | 99 |
|
85 | 100 | ```bash |
86 | | - Set JSON at key test_node. |
87 | | - value of node: 4303 |
| 101 | +$ node app.js |
88 | 102 | ``` |
89 | 103 |
|
| 104 | +You should see this output: |
| 105 | + |
| 106 | +```bash |
| 107 | +value of node: 4303 |
90 | 108 | ``` |
91 | | - % redis-cli |
92 | | - 127.0.0.1:6379> monitor |
93 | | - OK |
94 | 109 |
|
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 | + |
98 | 112 | ``` |
| 113 | +$ redis-cli |
| 114 | +127.0.0.1:6379> monitor |
| 115 | +OK |
99 | 116 |
|
| 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 | +``` |
100 | 120 |
|
101 | 121 | ### References |
102 | 122 |
|
| 123 | +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University |
103 | 124 | - [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) |
108 | 129 | - [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