You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -17,14 +17,242 @@ In this quickstart, you incorporate Azure Cache for Redis into a Node.js app to
17
17
## Prerequisites
18
18
19
19
- 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`.
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 computer.
21
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/).
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:
29
+
30
+
```bash
31
+
npm install redis
32
+
```
33
+
34
+
## Create a Node.js app to access a cache
35
+
36
+
Create a Node.js app that uses either Microsoft Entra ID or access keys to connect to an Azure Cache for Redis. We recommend you use Microsoft Entra ID.
37
+
38
+
## [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
### Install the JavaScript Azure Identity client library
43
+
44
+
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`:
45
+
46
+
```bash
47
+
npm install @azure/identity
48
+
```
49
+
50
+
### Create a new Node.js app using Microsoft Entra ID
51
+
52
+
1. Add environment variables for your **Host name** and **Service Principal ID**, which is the object ID of your Microsoft Entra ID service principal or user. In the Azure portal, this is shown as the _Username_.
53
+
54
+
```cmd
55
+
set AZURE_CACHE_FOR_REDIS_HOST_NAME=contosoCache
56
+
set REDIS_SERVICE_PRINCIPAL_ID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
57
+
```
58
+
59
+
1. Create a new script file named _redistest.js_.
60
+
61
+
1. Add the following example JavaScript to the file. 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/).
### Create a sample JavaScript app with reauthentication
144
+
145
+
Microsoft Entra ID access tokens have a limited lifespan, [averaging 75 minutes](/entra/identity-platform/configurable-token-lifetimes#token-lifetime-policies-for-access-saml-and-id-tokens). In order to maintain a connection to your cache, you need to refresh the token. This example demonstrates how to do this using JavaScript.
146
+
147
+
1. Create a new script file named _redistestreauth.js_.
148
+
149
+
1. Add the following example JavaScript to the file.
>For additional examples of using Microsoft Entra ID to authenticate to Redis using the node-redis library, please see [this GitHub repo](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md)
Add environment variables foryour **HOST NAME** and **Primary** access key. Use these variables from your code instead of including the sensitive information directlyin your code.
@@ -34,18 +262,15 @@ set AZURE_CACHE_FOR_REDIS_HOST_NAME=contosoCache
34
262
set AZURE_CACHE_FOR_REDIS_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
35
263
```
36
264
37
-
## Connect to the cache
265
+
### Connect to the cache
38
266
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.
267
+
>[!NOTE]
268
+
> Don't create a new connection for each operation in your code. Instead, reuse connections as much as possible.
269
+
>
40
270
41
-
## Create a new Node.js app
271
+
### Create a new Node.js app
42
272
43
-
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
-
```
273
+
1. Create a new script file named _redistest.js_.
49
274
50
275
1. Add the following example JavaScript to the file.
51
276
@@ -101,7 +326,7 @@ The latest builds of [node_redis](https://github.com/mranney/node_redis) provide
This code shows you how to connect to an Azure Cache forRedis instance using the cache host name and key environment variables. The code also stores and retrieves a string valuein 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/).
329
+
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
330
106
331
1. Run the script with Node.js.
107
332
@@ -111,7 +336,7 @@ The latest builds of [node_redis](https://github.com/mranney/node_redis) provide
111
336
112
337
1. Example the output.
113
338
114
-
```console
339
+
```bash
115
340
Cache command: PING
116
341
Cache response : PONG
117
342
@@ -130,31 +355,16 @@ The latest builds of [node_redis](https://github.com/mranney/node_redis) provide
130
355
Done
131
356
```
132
357
133
-
## Clean up resources
134
-
135
-
If you continue to the next tutorial, can keep the resources created in this quickstart and reuse them. Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.
136
-
137
-
> [!IMPORTANT]
138
-
> Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.
139
-
>
140
-
141
-
1. Sign in to the [Azure portal](https://portal.azure.com) and select **Resource groups**.
142
-
143
-
1. In the **Filter by name** text box, enter the name of your resource group. The instructions for this article used a resource group named *TestResources*. On your resource group in the result list, select **...** then **Delete resource group**.
Get the [Node.js quickstart](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/nodejs) on GitHub.
154
365
155
-
## Next steps
366
+
## Related content
156
367
157
368
In this quickstart, you learned how to use Azure Cache for Redis from a Node.js application. Continue to the next quickstart to use Azure Cache for Redis with an ASP.NET web app.
158
369
159
-
> [!div class="nextstepaction"]
160
-
> [Create an ASP.NET web app that uses an Azure Cache for Redis.](./cache-web-app-howto.md)
370
+
- [Create an ASP.NET web app that uses an Azure Cache for Redis.](cache-web-app-howto.md)
0 commit comments