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/howto-targetingfilter-javascript.md
+27-30Lines changed: 27 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,14 +18,14 @@ In this guide, you'll use the targeting filter to roll out a feature to targeted
18
18
19
19
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/).
20
20
- 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).
22
22
-[LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
23
23
24
24
## Create a web application with a feature flag
25
25
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.
27
27
28
-
### Initialize project setup
28
+
### Initial project setup
29
29
30
30
1. Create a folder called `targeting-filter-tutorial` and initialize the project.
31
31
@@ -43,9 +43,9 @@ In this section, you create a web application that uses the *Beta* feature flag
43
43
npm install express
44
44
```
45
45
46
-
### Add App Configuration provider
46
+
### Connect to Azure App Configuration
47
47
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.
49
49
50
50
1. Create a new file named *app.js* and add the following code.
51
51
@@ -62,30 +62,35 @@ You will load the feature flag you created in the prerequisites from Azure App C
62
62
let featureManager;
63
63
64
64
async functioninitializeConfig() {
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
+
}
70
72
}
71
-
}
72
-
});
73
+
});
74
+
} catch(error) {
75
+
console.error("Failed to load configuration:", error);
76
+
process.exit(1);
77
+
}
73
78
74
79
featureManager = new FeatureManager(new ConfigurationMapFeatureFlagProvider(appConfig));
75
80
}
76
-
```
77
-
78
-
### Use the feature flag
79
81
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
83
82
// Use a middleware to refresh the configuration before each request.
84
83
server.use((req, res, next) => {
85
84
appConfig.refresh();
86
85
next();
87
86
});
87
+
```
88
+
89
+
### Use the feature flag
88
90
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.
@@ -114,25 +119,21 @@ You will load the feature flag you created in the prerequisites from Azure App C
114
119
console.log(`Server is running at http://localhost:${port}`);
115
120
});
116
121
})
117
-
.catch((error) => {
118
-
console.error("Failed to load configuration:", error);
119
-
process.exit(1);
120
-
});
121
122
```
122
123
123
124
## Enable targeting for the web application
124
125
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.
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.
132
133
133
-
We will use ambient targeting context as an example in this tutorial.
134
+
You use ambient targeting context in this tutorial.
134
135
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).
136
137
137
138
```js
138
139
const express = require("express");
@@ -167,10 +168,6 @@ We will use ambient targeting context as an example in this tutorial.
167
168
// Existing code ...
168
169
```
169
170
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
-
174
171
1. When constructing the `FeatureManager`, pass the targeting context accessor to the `FeatureManagerOptions`.
0 commit comments