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
Copy file name to clipboardExpand all lines: articles/azure-app-configuration/enable-dynamic-configuration-javascript.md
+30-12Lines changed: 30 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,8 @@ Add the following key-value to your Azure App Configuration store. For more info
39
39
The following examples show how to use refreshable configuration values in console applications.
40
40
Choose the following instructions based on how your application consumes configuration data loaded from App Configuration, either as a `Map` or a configuration object.
41
41
42
+
### Load data from App Configuration
43
+
42
44
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).
43
45
44
46
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
181
183
182
184
---
183
185
186
+
### Run the application
187
+
184
188
1. Run your script:
185
189
186
190
```console
@@ -207,13 +211,13 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
207
211
Hello World - Updated!
208
212
```
209
213
210
-
## Web applications
214
+
## Server application
211
215
212
216
The following example shows how to update an existing http server to use refreshable configuration values.
213
217
214
218
1. Create a newjavascript file named `server.js` and add the following code:
215
219
216
-
```javascript
220
+
```javascript
217
221
const http = require('http');
218
222
219
223
function startServer() {
@@ -235,7 +239,7 @@ The following example shows how to update an existing http server to use refresh
235
239
236
240
1. Run your script:
237
241
238
-
```console
242
+
```console
239
243
node server.js
240
244
```
241
245
@@ -244,19 +248,21 @@ The following example shows how to update an existing http server to use refresh
244
248
> [!div class="mx-imgBorder"]
245
249
>
246
250
251
+
### Load data from App Configuration
252
+
247
253
1. Update the `server.js` to use App Configuration and enable dynamic refresh:
const credential = new DefaultAzureCredential(); // For more information, see https://learn.microsoft.com/azure/developer/javascript/sdk/credential-chains#use-defaultazurecredential-for-flexibility
256
262
257
-
let settings;
263
+
let appConfig;
258
264
async function initializeConfig() {
259
-
settings = await load(endpoint, credential, {
265
+
appConfig = await load(endpoint, credential, {
260
266
refreshOptions: {
261
267
enabled: true,
262
268
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
267
273
268
274
function startServer() {
269
275
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();
272
278
res.statusCode = 200;
273
279
res.setHeader('Content-Type', 'text/plain');
274
-
res.end(settings.get("message"));
280
+
res.end(appConfig.get("message"));
275
281
});
276
282
277
283
const hostname = "localhost";
@@ -286,9 +292,21 @@ The following example shows how to update an existing http server to use refresh
286
292
.then(() =>startServer());
287
293
```
288
294
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 newincoming 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
+
289
307
1. Relaunch your http server:
290
308
291
-
```console
309
+
```console
292
310
node server.js
293
311
```
294
312
@@ -302,7 +320,7 @@ The following example shows how to update an existing http server to use refresh
0 commit comments