Skip to content

Commit c16b3da

Browse files
add http server example
1 parent a0cf433 commit c16b3da

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

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

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Add the following key-value to your Azure App Configuration store. For more info
3434
> [!NOTE]
3535
> 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.
3636
37-
## Reload data from App Configuration
37+
## Console applications
3838

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.
@@ -181,8 +181,6 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
181181

182182
---
183183

184-
## Run the application
185-
186184
1. Run your script:
187185

188186
```console
@@ -209,6 +207,108 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
209207
Hello World - Updated!
210208
```
211209

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 new javascript 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+
> ![Screenshot of browser with a message.](./media/dynamic-refresh-javascript/http-server.png)
246+
247+
1. Update the `server.js` to use App Configuration and enable dynamic refresh:
248+
249+
``` javascript
250+
const http = require('http');
251+
252+
const { load } = require("@azure/app-configuration-provider");
253+
const { DefaultAzureCredential } = require("@azure/identity");
254+
const endpoint = process.env.AZURE_APPCONFIG_ENDPOINT;
255+
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+
> ![Screenshot of browser with a message.](./media/dynamic-refresh-javascript/http-server.png)
299+
300+
1. Update the following key-values to the Azure App Configuration store. Update value of the key `message` first and then `sentinel`.
301+
302+
| Key | Value | Label | Content type |
303+
|----------------|---------------------------|-------------|--------------------|
304+
| *message* | *Hello World - Updated!* | Leave empty | Leave empty |
305+
| *sentinel* | *2* | Leave empty | Leave empty |
306+
307+
1. After about 15 seconds, refresh the page and the message should be updated.
308+
309+
> [!div class="mx-imgBorder"]
310+
> ![Screenshot of browser with a message.](./media/dynamic-refresh-javascript/http-server-refreshed.png)
311+
212312
## Clean up resources
213313

214314
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
16.7 KB
Loading
15.8 KB
Loading

0 commit comments

Comments
 (0)