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
+103-3Lines changed: 103 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ Add the following key-value to your Azure App Configuration store. For more info
34
34
> [!NOTE]
35
35
> A *sentinel key* is a key that you update after you complete the change of all other keys. Your app monitors the sentinel key. When a change is detected, your app refreshes all configuration values. This approach helps to ensure the consistency of configuration in your app and reduces the overall number of requests made to your Azure App Configuration store, compared to monitoring all keys for changes.
36
36
37
-
## Reload data from App Configuration
37
+
## Console applications
38
38
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.
@@ -181,8 +181,6 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
181
181
182
182
---
183
183
184
-
## Run the application
185
-
186
184
1. Run your script:
187
185
188
186
```console
@@ -209,6 +207,108 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
209
207
Hello World - Updated!
210
208
```
211
209
210
+
## Web applications
211
+
212
+
The following example shows how to update an existing http server to use refreshable configuration values.
213
+
214
+
1. Create a newjavascript file named `server.js` and add the following code:
215
+
216
+
``` javascript
217
+
const http = require('http');
218
+
219
+
function startServer() {
220
+
const server = http.createServer((req, res) => {
221
+
res.statusCode = 200;
222
+
res.setHeader('Content-Type', 'text/plain');
223
+
res.end("Hello World!");
224
+
});
225
+
226
+
const hostname = "localhost";
227
+
const port = 3000;
228
+
server.listen(port, hostname, () => {
229
+
console.log(`Server running at http://localhost:${port}/`);
230
+
});
231
+
}
232
+
233
+
startServer()
234
+
```
235
+
236
+
1. Run your script:
237
+
238
+
``` console
239
+
node server.js
240
+
```
241
+
242
+
1. Visit `http://localhost:3000` and you will see the response:
243
+
244
+
> [!div class="mx-imgBorder"]
245
+
>
246
+
247
+
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
+
257
+
let settings;
258
+
async function initializeConfig() {
259
+
settings = await load(endpoint, credential, {
260
+
refreshOptions: {
261
+
enabled: true,
262
+
watchedSettings: [{ key: "sentinel" }], // Watch for changes to the key "sentinel" and refreshes the configuration when it changes
263
+
refreshIntervalInMs: 15_000 // Set the refresh interval
264
+
}
265
+
});
266
+
}
267
+
268
+
function startServer() {
269
+
const server = http.createServer((req, res) => {
270
+
// refresh the configuration setting whenever a request comes in
271
+
settings.refresh();
272
+
res.statusCode = 200;
273
+
res.setHeader('Content-Type', 'text/plain');
274
+
res.end(settings.get("message"));
275
+
});
276
+
277
+
const hostname = "localhost";
278
+
const port = 3000;
279
+
server.listen(port, hostname, () => {
280
+
console.log(`Server running at http://localhost:${port}/`);
281
+
});
282
+
}
283
+
284
+
// Initialize the configuration and then start the server
285
+
initializeConfig()
286
+
.then(() =>startServer());
287
+
```
288
+
289
+
1. Relaunch your http server:
290
+
291
+
``` console
292
+
node server.js
293
+
```
294
+
295
+
1. Visit `http://localhost:3000` and verify the response which is the `message` key in your App Configuration store.
296
+
297
+
> [!div class="mx-imgBorder"]
298
+
>
299
+
300
+
1. Update the following key-values to the Azure App Configuration store. Update value of the key `message` first and then `sentinel`.
0 commit comments