|
| 1 | +--- |
| 2 | +title: Enable conditional features with a custom filter in a Node.js application |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn how to implement a custom feature filter to enable conditional feature flags for your Node.js application. |
| 5 | +ms.service: azure-app-configuration |
| 6 | +ms.devlang: javascript |
| 7 | +ms.custom: devx-track-csharp |
| 8 | +author: zhiyuanliang-ms |
| 9 | +ms.author: zhiyuanliang |
| 10 | +ms.topic: how-to |
| 11 | +ms.custom: mode-other, devx-track-js |
| 12 | +ms.date: 09/26/2024 |
| 13 | +--- |
| 14 | + |
| 15 | +# Tutorial: Enable conditional features with a custom filter in a Node.js application |
| 16 | + |
| 17 | +Feature flags can use feature filters to enable features conditionally. To learn more about feature filters, see [Tutorial: Enable conditional features with feature filters](./howto-feature-filters.md). |
| 18 | + |
| 19 | +The example used in this tutorial is based on the Node.js application introduced in the feature management [quickstart](./quickstart-feature-flag-javascript.md). Before proceeding further, complete the quickstart to create a Node.js application with a *Beta* feature flag. Once completed, you must [add a custom feature filter](./howto-feature-filters.md) to the *Beta* feature flag in your App Configuration store. |
| 20 | + |
| 21 | +In this tutorial, you'll learn how to implement a custom feature filter and use the feature filter to enable features conditionally. |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- Create a [Node.js app with a feature flag](./quickstart-feature-flag-javascript.md). |
| 26 | +- [Add a custom feature filter to the feature flag](./howto-feature-filters.md) |
| 27 | + |
| 28 | +## Implement a custom feature filter |
| 29 | + |
| 30 | +You've added a custom feature filter named **Random** with a **Percentage** parameter for your *Beta* feature flag in the prerequisites. Next, you implement the feature filter to enable the *Beta* feature flag based on the chance defined by the **Percentage** parameter. |
| 31 | + |
| 32 | +1. Open the file *app.js* and add the `RandomFilter` with the following code. |
| 33 | + |
| 34 | + ``` javascript |
| 35 | + class RandomFilter { |
| 36 | + name = "Random"; |
| 37 | + evaluate(context) { |
| 38 | + const percentage = context.parameters.Percentage; |
| 39 | + const randomNumber = Math.random() * 100; |
| 40 | + return randomNumber <= percentage; |
| 41 | + } |
| 42 | + } |
| 43 | + ``` |
| 44 | + |
| 45 | + You added a `RandomFilter` class that has a single method named `evaluate`, which is called whenever a feature flag is evaluated. In `evaluate`, a feature filter enables a feature flag by returning `true`. |
| 46 | + |
| 47 | + You set the name to of `RandomFilter` to **Random**, which matches the filter name you set in the *Beta* feature flag in Azure App Configuration. |
| 48 | + |
| 49 | +1. Register the `RandomFilter` when creating the `FeatureManager`. |
| 50 | + |
| 51 | + ``` javascript |
| 52 | + const fm = new FeatureManager(ffProvider, {customFilters: [new RandomFilter()]}); |
| 53 | + ``` |
| 54 | + |
| 55 | +## Feature filter in action |
| 56 | + |
| 57 | +When you run the application the configuration provider will load the *Beta* feature flag from Azure App Configuration. The result of the `isEnabled("Beta")` method will be printed to the console. As the `RandomFilter` is implemented and used by the *Beta* feature flag, the result will be `True` 50 percent of the time and `False` the other 50 percent of the time. |
| 58 | + |
| 59 | +Running the application will show that the *Beta* feature flag is sometimes enabled and sometimes not. |
| 60 | + |
| 61 | +``` bash |
| 62 | +Beta is enabled: true |
| 63 | +Beta is enabled: false |
| 64 | +Beta is enabled: false |
| 65 | +Beta is enabled: true |
| 66 | +Beta is enabled: true |
| 67 | +Beta is enabled: false |
| 68 | +Beta is enabled: false |
| 69 | +Beta is enabled: false |
| 70 | +Beta is enabled: true |
| 71 | +Beta is enabled: true |
| 72 | +``` |
| 73 | + |
| 74 | +## Next steps |
| 75 | + |
| 76 | +To learn more about the built-in feature filters, continue to the following tutorials. |
| 77 | + |
| 78 | +> [!div class="nextstepaction"] |
| 79 | +> [Enable features on a schedule](./howto-timewindow-filter.md) |
| 80 | + |
| 81 | +> [!div class="nextstepaction"] |
| 82 | +> [Roll out features to targeted audience](./howto-targetingfilter.md) |
0 commit comments