Skip to content

Commit 10254aa

Browse files
update
1 parent 34adeaf commit 10254aa

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

articles/azure-app-configuration/enable-dynamic-configuration-javascript.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Add the following key-value to your Azure App Configuration store. For more info
3939
The following examples show how to use refreshable configuration values in console applications.
4040
Choose the following instructions based on how your application consumes configuration data loaded from App Configuration, either as a `Map` or a configuration object.
4141

42+
### Load data from App Configuration
43+
4244
You can connect to App Configuration using either Microsoft Entra ID (recommended) or a connection string. The following code snippet demonstrates using Microsoft Entra ID. You use the DefaultAzureCredential to authenticate to your App Configuration store. While completing the quickstart listed in the prerequisites, you already [assigned your credential the App Configuration Data Reader role](./concept-enable-rbac.md#authentication-with-token-credentials).
4345

4446
1. Open the file *app.js* and update the `load` function. Add a `refreshOptions` parameter to enable the refresh and configure refresh options. The loaded configuration will be updated when a change is detected on the server. By default, a refresh interval of 30 seconds is used, but you can override it with the `refreshIntervalInMs` property.
@@ -181,6 +183,8 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
181183

182184
---
183185

186+
### Run the application
187+
184188
1. Run your script:
185189

186190
```console
@@ -207,13 +211,13 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
207211
Hello World - Updated!
208212
```
209213

210-
## Web applications
214+
## Server application
211215

212216
The following example shows how to update an existing http server to use refreshable configuration values.
213217

214218
1. Create a new javascript file named `server.js` and add the following code:
215219

216-
``` javascript
220+
```javascript
217221
const http = require('http');
218222
219223
function startServer() {
@@ -235,7 +239,7 @@ The following example shows how to update an existing http server to use refresh
235239

236240
1. Run your script:
237241

238-
``` console
242+
```console
239243
node server.js
240244
```
241245

@@ -244,19 +248,21 @@ The following example shows how to update an existing http server to use refresh
244248
> [!div class="mx-imgBorder"]
245249
> ![Screenshot of browser with a message.](./media/dynamic-refresh-javascript/http-server.png)
246250

251+
### Load data from App Configuration
252+
247253
1. Update the `server.js` to use App Configuration and enable dynamic refresh:
248254

249-
``` javascript
250-
const http = require('http');
255+
```javascript
256+
const http = require("http");
251257
252258
const { load } = require("@azure/app-configuration-provider");
253259
const { DefaultAzureCredential } = require("@azure/identity");
254260
const endpoint = process.env.AZURE_APPCONFIG_ENDPOINT;
255261
const credential = new DefaultAzureCredential(); // For more information, see https://learn.microsoft.com/azure/developer/javascript/sdk/credential-chains#use-defaultazurecredential-for-flexibility
256262
257-
let settings;
263+
let appConfig;
258264
async function initializeConfig() {
259-
settings = await load(endpoint, credential, {
265+
appConfig = await load(endpoint, credential, {
260266
refreshOptions: {
261267
enabled: true,
262268
watchedSettings: [{ key: "sentinel" }], // Watch for changes to the key "sentinel" and refreshes the configuration when it changes
@@ -267,11 +273,11 @@ The following example shows how to update an existing http server to use refresh
267273
268274
function startServer() {
269275
const server = http.createServer((req, res) => {
270-
// refresh the configuration setting whenever a request comes in
271-
settings.refresh();
276+
// refresh the configuration asynchronously when there is any incoming request
277+
appConfig.refresh();
272278
res.statusCode = 200;
273279
res.setHeader('Content-Type', 'text/plain');
274-
res.end(settings.get("message"));
280+
res.end(appConfig.get("message"));
275281
});
276282
277283
const hostname = "localhost";
@@ -286,9 +292,21 @@ The following example shows how to update an existing http server to use refresh
286292
.then(() => startServer());
287293
```
288294

295+
### Request-driven configuration refresh
296+
297+
In most cases, the refresh operation of the App Configuration provider can be treated as a no-op. It will only send requests to check the value in App Configuration when the refresh interval time you set has passed.
298+
299+
We recommend to implement request-driven configuration refresh for your web application. The configuration refresh is triggered by the incoming requests to your web app. No refresh will occur if your app is idle, when there is no request incoming. When your app is active, you can use a middleware or similar mechanism to trigger the `appConfig.refresh()` call upon every incoming request to your application.
300+
301+
- If a request to App Configuration for change detection fails, your app will continue to use the cached configuration. New attempts to check for changes will be made periodically while there are new incoming requests to your app.
302+
303+
- The configuration refresh happens asynchronously to the processing of your app's incoming requests. It will not block or slow down the incoming request that triggered the refresh. The request that triggered the refresh may not get the updated configuration values, but later requests will get new configuration values.
304+
305+
### Run the application
306+
289307
1. Relaunch your http server:
290308
291-
``` console
309+
```console
292310
node server.js
293311
```
294312
@@ -302,7 +320,7 @@ The following example shows how to update an existing http server to use refresh
302320
| Key | Value | Label | Content type |
303321
|----------------|---------------------------|-------------|--------------------|
304322
| *message* | *Hello World - Updated!* | Leave empty | Leave empty |
305-
| *sentinel* | *2* | Leave empty | Leave empty |
323+
| *sentinel* | *3* | Leave empty | Leave empty |
306324
307325
1. After about 15 seconds, refresh the page and the message should be updated.
308326

0 commit comments

Comments
 (0)