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/how-to-targetingfilter-javascript.md
+57-48Lines changed: 57 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ A _Beta_ feature flag with targeting filter. [Create the feature flag](./howto-t
25
25
26
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
-
### Initial project setup
28
+
### Setup a Node.js Express project
29
29
30
30
1. Create a folder called `targeting-filter-tutorial` and initialize the project.
31
31
@@ -43,75 +43,60 @@ In this section, you create a web application that uses the [_Beta_ feature flag
43
43
npm install express
44
44
```
45
45
46
-
### Connect to Azure App Configuration
47
-
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
-
50
46
1. Create a new file named *app.js* and add the following code.
51
47
52
48
```js
53
49
const express = require("express");
54
50
const server = express();
55
51
const port = "8080";
56
52
53
+
server.listen(port, () => {
54
+
console.log(`Server is running at http://localhost:${port}`);
55
+
});
56
+
```
57
+
58
+
### Connect to Azure App Configuration
59
+
60
+
1. Update the *app.js* and add the following code.
appConfig = await load(appConfigEndpoint, new DefaultAzureCredential(), {
67
-
featureFlagOptions: {
68
-
enabled: true,
69
-
refresh: {
70
-
enabled: true
71
-
}
73
+
appConfig = await load(appConfigEndpoint, new DefaultAzureCredential(), {
74
+
featureFlagOptions: {
75
+
enabled: true,
76
+
refresh: {
77
+
enabled: true
72
78
}
73
-
});
74
-
} catch(error) {
75
-
console.error("Failed to load configuration:", error);
76
-
process.exit(1);
77
-
}
79
+
}
80
+
});
78
81
79
-
featureManager = new FeatureManager(new ConfigurationMapFeatureFlagProvider(appConfig));
82
+
const ffProvider = new ConfigurationMapFeatureFlagProvider(appConfig);
83
+
featureManager = new FeatureManager(ffProvider);
80
84
}
81
85
82
86
// Use a middleware to refresh the configuration before each request.
83
87
server.use((req, res, next) => {
84
88
appConfig.refresh();
85
89
next();
86
90
});
91
+
// Existing code ...
87
92
```
88
93
89
-
### Use the feature flag
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.
94
+
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.
1. Complete the application. It will initialize the configuration and then start the express server.
96
+
1. Update the code to ensure the Express server starts only after the configuration has been successfully initialized.
113
97
114
98
```js
99
+
// Existing code ...
115
100
initializeConfig()
116
101
.then(() => {
117
102
// Start the express server.
@@ -121,6 +106,33 @@ You connect to Azure App Configuration to load feature flags, enable automatic r
121
106
})
122
107
```
123
108
109
+
### Use the feature flag
110
+
111
+
Add the following code to the *app.js* file to configure the route handler for the Express server. The server will serve different contents based on whether the **Beta** feature flag is enabled.
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.
@@ -171,12 +183,12 @@ You use ambient targeting context in this tutorial.
171
183
1. When constructing the `FeatureManager`, pass the targeting context accessor to the `FeatureManagerOptions`.
172
184
173
185
```js
174
-
featureManager = new FeatureManager(featureFlagProvider, {
0 commit comments