Skip to content

Commit 357937d

Browse files
authored
Add EntraID to Node Quickstart
1 parent 5454b5e commit 357937d

File tree

1 file changed

+115
-11
lines changed

1 file changed

+115
-11
lines changed

articles/azure-cache-for-redis/cache-nodejs-get-started.md

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,121 @@ In this quickstart, you incorporate Azure Cache for Redis into a Node.js app to
1717
## Prerequisites
1818

1919
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
20-
- [node_redis](https://github.com/mranney/node_redis), which you can install with the command `npm install redis`.
21-
22-
For examples of using other Node.js clients, see the individual documentation for the Node.js clients listed at [Node.js Redis clients](https://redis.io/docs/connect/clients/nodejs/).
20+
- Node.js installed, if you haven't done so already. See [Install Node.js on Windows](../../windows/dev-environment/javascript/nodejs-on-windows) for instructions on how to install Node and NPM on a Windows machine.
2321

2422
## Create a cache
2523

2624
[!INCLUDE [redis-cache-create](~/reusable-content/ce-skilling/azure/includes/azure-cache-for-redis/includes/redis-cache-create.md)]
2725

26+
## Install the node-redis client lbirary
27+
The [node-redis](https://github.com/redis/node-redis) library is the primary Node.js client for Redis. You can install the client with [npm](https://docs.npmjs.com/about-npm) by using the following command:
28+
29+
```bash
30+
npm install redis
31+
```
32+
33+
## [Microsoft EntraID Authentication (recommended)](#tab/entraid)
34+
35+
## Enable Microsoft EntraID and add a User or Service Principal
36+
<--Fran, we probably need an include file on enabling EntraID-->
37+
Blah blah blah, do the steps listed [here](cache-azure-active-directory-for-authentication)
38+
39+
## Install the JavaScript Azure Identity client library
40+
The [Microsoft Authentication Library (MSAL)](../../entra/identity-platform/msal-overview) allows you to acquire security tokens from Microsoft identity to authenticate users. There's a [Javascript Azure identity client library](../../javascript/api/overview/azure/identity-readme) available that uses MSAL to provide token authentication support. Install this library using `npm`:
41+
42+
```bash
43+
npm install @azure/identity
44+
```
45+
46+
## Create a new Node.js app
47+
48+
1. Create a new script file named *redistest.js*.
49+
1. Add the following example JavaScript to the file.
50+
51+
```node
52+
const { createClient } = require("redis");
53+
const { DefaultAzureCredential } = require("@azure/identity");
54+
55+
async function main() {
56+
// Construct a Token Credential from Identity library, e.g. ClientSecretCredential / ClientCertificateCredential / ManagedIdentityCredential, etc.
57+
const credential = new DefaultAzureCredential();
58+
const redisScope = "https://redis.azure.com/.default";
59+
60+
// Fetch a Microsoft Entra token to be used for authentication. This token will be used as the password.
61+
let accessToken = await credential.getToken(redisScope);
62+
console.log("access Token", accessToken);
63+
64+
// Create redis client and connect to the Azure Cache for Redis over the TLS port using the access token as password.
65+
const cacheConnection = createClient({
66+
username: process.env.REDIS_SERVICE_PRINCIPAL_NAME,
67+
password: accessToken.token,
68+
url: `redis://${process.env.REDIS_HOSTNAME}:6380`,
69+
pingInterval: 100000,
70+
socket: {
71+
tls: true,
72+
keepAlive: 0
73+
},
74+
});
75+
76+
cacheConnection.on("error", (err) => console.log("Redis Client Error", err));
77+
await cacheConnection.connect();
78+
79+
// PING command
80+
console.log("\nCache command: PING");
81+
console.log("Cache response : " + await cacheConnection.ping());
82+
83+
// SET
84+
console.log("\nCache command: SET Message");
85+
console.log("Cache response : " + await cacheConnection.set("Message",
86+
"Hello! The cache is working from Node.js!"));
87+
88+
// GET
89+
console.log("\nCache command: GET Message");
90+
console.log("Cache response : " + await cacheConnection.get("Message"));
91+
92+
// Client list, useful to see if connection list is growing...
93+
console.log("\nCache command: CLIENT LIST");
94+
console.log("Cache response : " + await cacheConnection.sendCommand(["CLIENT", "LIST"]));
95+
96+
cacheConnection.disconnect();
97+
98+
return "Done"
99+
}
100+
101+
main().then((result) => console.log(result)).catch(ex => console.log(ex));
102+
```
103+
104+
This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The `PING` and `CLIENT LIST` commands are also executed. For more examples of using Redis with the [node-redis](https://github.com/redis/node-redis) client, see [https://redis.js.org/](https://redis.js.org/).
105+
106+
1. Run the script with Node.js.
107+
108+
```bash
109+
node redistest.js
110+
```
111+
112+
1. Example the output.
113+
114+
```console
115+
Cache command: PING
116+
Cache response : PONG
117+
118+
Cache command: GET Message
119+
Cache response : Hello! The cache is working from Node.js!
120+
121+
Cache command: SET Message
122+
Cache response : OK
123+
124+
Cache command: GET Message
125+
Cache response : Hello! The cache is working from Node.js!
126+
127+
Cache command: CLIENT LIST
128+
Cache response : id=10017364 addr=76.22.73.183:59380 fd=221 name= age=1 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 ow=0 owmem=0 events=r cmd=client user=default numops=6
129+
130+
Done
131+
```
132+
133+
## [Access Key Authentication](#tab/accesskey)
134+
28135
[!INCLUDE [redis-cache-access-keys](includes/redis-cache-access-keys.md)]
29136

30137
Add environment variables for your **HOST NAME** and **Primary** access key. Use these variables from your code instead of including the sensitive information directly in your code.
@@ -36,17 +143,13 @@ set AZURE_CACHE_FOR_REDIS_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
36143

37144
## Connect to the cache
38145

39-
The latest builds of [node_redis](https://github.com/mranney/node_redis) provide support several connection options. Don't create a new connection for each operation in your code. Instead, reuse connections as much as possible.
146+
>[!NOTE]
147+
> Don't create a new connection for each operation in your code. Instead, reuse connections as much as possible.
148+
>
40149
41150
## Create a new Node.js app
42151
43152
1. Create a new script file named *redistest.js*.
44-
1. Use the command to install a redis package.
45-
46-
```bash
47-
`npm install redis`
48-
```
49-
50153
1. Add the following example JavaScript to the file.
51154
52155
```javascript
@@ -101,7 +204,7 @@ The latest builds of [node_redis](https://github.com/mranney/node_redis) provide
101204
testCache().then((result) => console.log(result)).catch(ex => console.log(ex));
102205
```
103206
104-
This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The `PING` and `CLIENT LIST` commands are also executed. For more examples of using Redis with the [node_redis](https://github.com/mranney/node_redis) client, see [https://redis.js.org/](https://redis.js.org/).
207+
This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The `PING` and `CLIENT LIST` commands are also executed. For more examples of using Redis with the [node_redis](https://github.com/redis/node-redis) client, see [https://redis.js.org/](https://redis.js.org/).
105208
106209
1. Run the script with Node.js.
107210
@@ -129,6 +232,7 @@ The latest builds of [node_redis](https://github.com/mranney/node_redis) provide
129232
130233
Done
131234
```
235+
---
132236
133237
## Clean up resources
134238

0 commit comments

Comments
 (0)