Skip to content

Commit af1be82

Browse files
update
1 parent 7a89580 commit af1be82

File tree

5 files changed

+1613
-0
lines changed

5 files changed

+1613
-0
lines changed

examples/web-example/.env.template

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# You can define environment variables in .env file and load them with 'dotenv' package.
2+
# This is a template of related environment variables in examples.
3+
# To use this file directly, please rename it to .env
4+
APPCONFIG_CONNECTION_STRING=<app-configuration-connection-string>
5+
6+
APPCONFIG_ENDPOINT=<app-configuration-endpoint>
7+
8+
# Credential used to authenticate using Microsoft Entra ID (Azure AD) role-based authentication.
9+
# https://docs.microsoft.com/javascript/api/@azure/identity/environmentcredential
10+
AZURE_TENANT_ID=<AD tenant id or name>
11+
AZURE_CLIENT_ID=<ID of the user/service principal to authenticate as>
12+
AZURE_CLIENT_SECRET=<client secret used to authenticate to Azure AD>

examples/web-example/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Examples for Azure App Configuration JavaScript Provider
2+
3+
These examples show how to use the JavaScript Provider for Azure App Configuration in some common scenarios.
4+
5+
## Prerequisites
6+
7+
The examples are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
8+
9+
You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run the examples:
10+
11+
- [Azure App Configuration store](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-azure-app-configuration-create?tabs=azure-portal)
12+
13+
The examples retrieve credentials to access your App Configuration store from environment variables.
14+
Alternatively, edit the source code to include the appropriate credentials.
15+
See each individual example for details on which environment variables/credentials it requires to function.
16+
17+
## Add a key-value
18+
Add the following key-value to the App Configuration store and leave **Label** and **Content Type** with their default values. For more information about how to add key-values to a store using the Azure portal or the CLI, go to [Create a key-value](./quickstart-azure-app-configuration-create.md#create-a-key-value).
19+
20+
| Key | Value |
21+
|-----------|----------------|
22+
| *message* | *Hello World!* |
23+
24+
## Setup & Run
25+
26+
To run the examples using the published version of the package:
27+
28+
1. Install the dependencies using `npm`:
29+
30+
```bash
31+
npm install
32+
```
33+
34+
2. There are two ways to run the examples using correct credentials:
35+
36+
- Edit the file `.env.template`, adding the access keys to your App Configuration store. and rename the file from `.env.template` to just `.env`. The examples will read this file automatically.
37+
38+
3. Run the examples:
39+
```bash
40+
node httpserver.mjs
41+
```
42+
43+
Go to `http://localhost:3000`
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import * as dotenv from "dotenv";
5+
dotenv.config()
6+
7+
import { load } from "@azure/app-configuration-provider";
8+
import { DefaultAzureCredential } from "@azure/identity";
9+
const endpoint = process.env.APPCONFIG_ENDPOINT;
10+
const credential = new DefaultAzureCredential();
11+
const appConfig = await load(endpoint, credential, {
12+
refreshOptions: {
13+
enabled: true,
14+
// By default, the refresh interval is 30 seconds. You can change it by setting refreshIntervalInMs.
15+
},
16+
keyVaultOptions:{
17+
credential: credential
18+
}
19+
});
20+
let config = appConfig.constructConfigurationObject();
21+
22+
appConfig.onRefresh(() => {
23+
config = appConfig.constructConfigurationObject();
24+
});
25+
26+
import express from "express";
27+
28+
const server = express();
29+
const PORT = 3000;
30+
31+
server.use(express.json());
32+
33+
// Use a middleware to achieve request-driven configuration refresh
34+
// For more information, please go to dynamic refresh tutorial: https://learn.microsoft.com/azure/azure-app-configuration/enable-dynamic-configuration-javascript
35+
server.use((req, res, next) => {
36+
// this call is not blocking, the configuration will be updated asynchronously
37+
appConfig.refresh();
38+
next();
39+
});
40+
41+
server.get("/", (req, res) => {
42+
res.send(`Message from Azure App Configuration: ${appConfig.get("message")}`);
43+
});
44+
45+
server.get("/config", (req, res) => {
46+
res.json(config);
47+
});
48+
49+
server.listen(PORT, () => {
50+
console.log(`Server is running on http://localhost:${PORT}`);
51+
});

0 commit comments

Comments
 (0)