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
@@ -65,45 +73,46 @@ In this section, you create a web application that uses the *Beta* feature flag
65
73
66
74
featureManager = new FeatureManager(new ConfigurationMapFeatureFlagProvider(appConfig));
67
75
}
76
+
```
77
+
78
+
### Use the feature flag
68
79
69
-
functionbuildContent(title, message) {
70
-
return`
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
+
// Use a middleware to refresh the configuration before each request.
buildContent("Beta Page", "This is a beta page.") :
93
-
buildContent("Home Page", "Welcome.")
94
-
);
95
-
});
102
+
</html>`
103
+
);
104
+
});
105
+
```
96
106
97
-
const port = "8080";
98
-
app.listen(port, () => {
99
-
console.log(`Server is running at http://localhost:${port}`);
100
-
});
101
-
}
107
+
1. Complete the application. It will initialize the configuration and then start the express server.
102
108
103
-
// Initialize the configuration and start the server
109
+
```js
104
110
initializeConfig()
105
111
.then(() => {
106
-
startServer();
112
+
// Start the express server.
113
+
server.listen(port, () => {
114
+
console.log(`Server is running at http://localhost:${port}`);
115
+
});
107
116
})
108
117
.catch((error) => {
109
118
console.error("Failed to load configuration:", error);
@@ -113,33 +122,36 @@ In this section, you create a web application that uses the *Beta* feature flag
113
122
114
123
## Enable targeting for the web application
115
124
116
-
A targeting context is required for feature evaluation with targeting. You can provide it as a parameter to the `featureManager.isEnabled` API explicitly. In the tutorial, we extract user information from query parameters of the request URL for simplicity.
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.
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.
119
132
120
-
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.
133
+
We will use ambient targeting context as an example in this tutorial.
121
134
122
-
1. Add the following code to your application:
135
+
1. Add the following code after where you declare the express server.
// Store the request in AsyncLocalStorage for this request chain
143
+
// Use a middleware to store request context.
144
+
server.use((req, res, next) => {
145
+
// Store the request in AsyncLocalStorage for this request chain.
134
146
requestAccessor.run(req, () => {
135
147
next();
136
148
});
137
149
});
138
150
139
-
// Create a targeting context accessor that retrieves user data from the current request
151
+
// Create a targeting context accessor that retrieves user data from the current request.
140
152
const targetingContextAccessor = {
141
153
getTargetingContext: () => {
142
-
// Get the current request from AsyncLocalStorage
154
+
// Get the current request from AsyncLocalStorage.
143
155
const request = requestAccessor.getStore();
144
156
if (!request) {
145
157
return undefined;
@@ -151,35 +163,114 @@ In the web application, the targeting context can also be provided as an ambient
151
163
};
152
164
}
153
165
};
154
-
155
-
// existing code
156
-
// ...
166
+
// Existing code ...
157
167
```
158
168
159
-
1. When constructing the `FeatureManager`, pass the targeting cotnext accessor to the `FeatureManagerOptions`.
169
+
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.
170
+
171
+
For more information, go to [Using AsyncLocalStorage for request context](./feature-management-javascript-reference.md#using-asynclocalstorage-for-request-context).
172
+
173
+
1. When constructing the `FeatureManager`, pass the targeting context accessor to the `FeatureManagerOptions`.
160
174
161
175
```js
162
176
featureManager = new FeatureManager(featureFlagProvider, {
1. Instead of explicitly passing the targeting context with each `isEnabled` call, the feature manager will retrieve the current user's targeting information from the targeting context accessor.
181
+
Your complete application code should look like the following:
// Initialize the configuration and start the server
261
+
initializeConfig()
262
+
.then(() => {
263
+
// Start the express server.
264
+
server.listen(port, () => {
265
+
console.log(`Server is running at http://localhost:${port}`);
179
266
});
180
-
```
267
+
})
268
+
.catch((error) => {
269
+
console.error("Failed to load configuration:", error);
270
+
process.exit(1);
271
+
});
272
+
```
181
273
182
-
For more information, go to [Using AsyncLocalStorage for request context](./feature-management-javascript-reference.md#using-asynclocalstorage-for-request-context).
0 commit comments