Skip to content

Commit ca7890a

Browse files
update
1 parent c2bcbe7 commit ca7890a

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

articles/azure-app-configuration/howto-targetingfilter-javascript.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ In this guide, you'll use the targeting filter to roll out a feature to targeted
1818

1919
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/).
2020
- An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store).
21-
- A feature flag with targeting filter. [Create the feature flag](./howto-targetingfilter.md).
21+
A _Beta_ feature flag with targeting filter. [Create the feature flag](./howto-targetingfilter.md).
2222
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
2323

2424
## Create a web application with a feature flag
2525

26-
In this section, you create a web application that uses the *Beta* feature flag to control the access to the beta version of the page.
26+
In this section, you create a web application that uses the [_Beta_ feature flag](./howto-targetingfilter.md) to control the access to the beta version of a web page.
2727

28-
### Initialize project setup
28+
### Initial project setup
2929

3030
1. Create a folder called `targeting-filter-tutorial` and initialize the project.
3131

@@ -43,9 +43,9 @@ In this section, you create a web application that uses the *Beta* feature flag
4343
npm install express
4444
```
4545

46-
### Add App Configuration provider
46+
### Connect to Azure App Configuration
4747

48-
You will load the feature flag you created in the prerequisites from Azure App Configuration and create a `FeatureManager` to manage the feature flag.
48+
You connect to Azure App Configuration to load feature flags, enable automatic refresh, and create a `FeatureManager` object for accessing feature flags later. A middleware is added to refresh configuration before each request.
4949

5050
1. Create a new file named *app.js* and add the following code.
5151

@@ -62,30 +62,35 @@ You will load the feature flag you created in the prerequisites from Azure App C
6262
let featureManager;
6363
6464
async function initializeConfig() {
65-
appConfig = await load(appConfigEndpoint, new DefaultAzureCredential(), {
66-
featureFlagOptions: {
67-
enabled: true,
68-
refresh: {
69-
enabled: true
65+
try {
66+
appConfig = await load(appConfigEndpoint, new DefaultAzureCredential(), {
67+
featureFlagOptions: {
68+
enabled: true,
69+
refresh: {
70+
enabled: true
71+
}
7072
}
71-
}
72-
});
73+
});
74+
} catch(error) {
75+
console.error("Failed to load configuration:", error);
76+
process.exit(1);
77+
}
7378
7479
featureManager = new FeatureManager(new ConfigurationMapFeatureFlagProvider(appConfig));
7580
}
76-
```
77-
78-
### Use the feature flag
7981
80-
1. Add the following code to the *app.js* file. You add a middleware to refresh configuration and configure the route handler. The server will serve different contents based on whether the **Beta** feature flag is enabled.
81-
82-
```js
8382
// Use a middleware to refresh the configuration before each request.
8483
server.use((req, res, next) => {
8584
appConfig.refresh();
8685
next();
8786
});
87+
```
88+
89+
### Use the feature flag
8890
91+
1. Add the following code to the *app.js* file. You configure the route handler so that the server will serve different contents based on whether the **Beta** feature flag is enabled.
92+
93+
```js
8994
server.get("/", async (req, res) => {
9095
const isBetaEnabled = await featureManager.isEnabled("Beta");
9196
const [title, message] = isBetaEnabled
@@ -114,25 +119,21 @@ You will load the feature flag you created in the prerequisites from Azure App C
114119
console.log(`Server is running at http://localhost:${port}`);
115120
});
116121
})
117-
.catch((error) => {
118-
console.error("Failed to load configuration:", error);
119-
process.exit(1);
120-
});
121122
```
122123
123124
## Enable targeting for the web application
124125
125-
A targeting context is required for feature evaluation with targeting. To explicitly specify the targeting context for each feature evaluation, you can pass targeting context as a parameter to the `featureManager.isEnabled` call.
126+
A targeting context is required when evaluating features with targeting enabled. To explicitly provide this context for feature evaluation, you can pass it as a parameter to the `featureManager.isEnabled` method.
126127
127128
```js
128129
const isBetaEnabled = await featureManager.isEnabled("Beta", { userId: "UserA", groups: ["Group1"] });
129130
```
130131
131-
In the web application, the targeting context can also be provided as an ambient context by implementing the [ITargetingContextAccessor](./feature-management-javascript-reference.md#itargetingcontextaccessor) interface. Ambient targeting context means the targeting information is automatically retrieved from the environment (like the current HTTP request) without explicitly passing it to each `featureManager.isEnabled()` call.
132+
In a web application, the targeting context can also be provided as an ambient context by implementing the [ITargetingContextAccessor](./feature-management-javascript-reference.md#itargetingcontextaccessor) interface. An ambient targeting context means that targeting information is automatically retrieved from the environment, such as the current HTTP request, without needing to explicitly pass it to each `featureManager.isEnabled()` call.
132133
133-
We will use ambient targeting context as an example in this tutorial.
134+
You use ambient targeting context in this tutorial.
134135
135-
1. Add the following code after the express server declaration
136+
1. Add the following code after the Express server declaration. It uses `AsyncLocalStorage` to store the current request, allowing the feature manager to automatically retrieve the targeting context via a targeting context accessor callback. For more details, see [Using AsyncLocalStorage for request context](./feature-management-javascript-reference.md#using-asynclocalstorage-for-request-context).
136137
137138
```js
138139
const express = require("express");
@@ -167,10 +168,6 @@ We will use ambient targeting context as an example in this tutorial.
167168
// Existing code ...
168169
```
169170
170-
For demonstration purposes, in this tutorial, you extract user information from the query string of the request URL. In a real-world application, user information is typically obtained through authentication mechanisms.
171-
172-
For more information, go to [Using AsyncLocalStorage for request context](./feature-management-javascript-reference.md#using-asynclocalstorage-for-request-context).
173-
174171
1. When constructing the `FeatureManager`, pass the targeting context accessor to the `FeatureManagerOptions`.
175172
176173
```js

0 commit comments

Comments
 (0)