|
| 1 | +--- |
| 2 | +title: Quickstart for adding feature flags to JavaScript apps |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: In this quickstart, add feature flags to a Node.js app and manage them using Azure App Configuration. |
| 5 | +author: zhiyuanliang-ms |
| 6 | +ms.service: azure-app-configuration |
| 7 | +ms.devlang: javascript |
| 8 | +ms.topic: quickstart |
| 9 | +ms.date: 09/26/2024 |
| 10 | +ms.author: zhiyuanliang |
| 11 | +ms.custom: quickstart, mode-other, devx-track-js |
| 12 | +#Customer intent: As an JavaScript developer, I want to use feature flags to control feature availability quickly and confidently. |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Add feature flags to a Python app |
| 16 | + |
| 17 | +In this quickstart, you incorporate Azure App Configuration into a Node.js console app to create an end-to-end implementation of feature management. You can use App Configuration to centrally store all your feature flags and control their states. |
| 18 | + |
| 19 | +The JavaScript Feature Management libraries extend the framework with feature flag support. They seamlessly integrate with App Configuration through its JavaScript configuration provider. |
| 20 | + |
| 21 | +The example used in this tutorial is based on the Node.js application introduced in the [quickstart](./quickstart-javascript-provider.md). |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- Finish the quickstart [Create a JavaScript app with Azure App Configuration](./quickstart-javascript-provider.md). |
| 26 | + |
| 27 | +## Add a feature flag |
| 28 | + |
| 29 | +Add a feature flag called *Beta* to the App Configuration store and leave **Label** and **Description** with their default values. For more information about how to add feature flags to a store using the Azure portal or the CLI, go to [Create a feature flag](./manage-feature-flags.md#create-a-feature-flag). |
| 30 | + |
| 31 | +> [!div class="mx-imgBorder"] |
| 32 | +>  |
| 33 | +
|
| 34 | +## Use the feature flag |
| 35 | + |
| 36 | +1. Go to the *app-configuration-quickstart* directory that you created in the quickstart and install the Feature Management by using the `npm install` command. |
| 37 | + |
| 38 | + ```console |
| 39 | + npm install @microsoft/feature-management |
| 40 | + ``` |
| 41 | + |
| 42 | +1. Open the file *app.js* and update it with the following code. |
| 43 | + |
| 44 | + ``` javascript |
| 45 | + const sleepInMs = require("util").promisify(setTimeout); |
| 46 | + const { load } = require("@azure/app-configuration-provider"); |
| 47 | + const { FeatureManager, ConfigurationMapFeatureFlagProvider} = require("@microsoft/feature-management") |
| 48 | + const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING; |
| 49 | + |
| 50 | + async function run() { |
| 51 | + // Connecting to Azure App Configuration using connection string |
| 52 | + const settings = await load(connectionString, { |
| 53 | + featureFlagOptions: { |
| 54 | + enabled: true, |
| 55 | + // Note: selectors must be explicitly provided for feature flags. |
| 56 | + selectors: [{ |
| 57 | + keyFilter: "*" |
| 58 | + }], |
| 59 | + refresh: { |
| 60 | + enabled: true, |
| 61 | + refreshIntervalInMs: 10_000 |
| 62 | + } |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + // Creating a feature flag provider which uses a map as feature flag source |
| 67 | + const ffProvider = new ConfigurationMapFeatureFlagProvider(settings); |
| 68 | + // Creating a feature manager which will evaluate the feature flag |
| 69 | + const fm = new FeatureManager(ffProvider); |
| 70 | + |
| 71 | + while (true) { |
| 72 | + await settings.refresh(); // Refresh to get the latest feature flag settings |
| 73 | + const isEnabled = await fm.isEnabled("Beta"); // Evaluate the feature flag |
| 74 | + console.log(`Beta is enabled: ${isEnabled}`); |
| 75 | + await sleepInMs(5000); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + run().catch(console.error); |
| 80 | + ``` |
| 81 | + |
| 82 | +## Run the application |
| 83 | + |
| 84 | +1. Run your script: |
| 85 | + |
| 86 | + ``` console |
| 87 | + node app.js |
| 88 | + ``` |
| 89 | + |
| 90 | +1. You will see the following console outputs because the *Beta feature flag is disabled. |
| 91 | + |
| 92 | + ``` console |
| 93 | + Beta is enabled: false |
| 94 | + ``` |
| 95 | + |
| 96 | +1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and select the App Configuration store that you created previously. |
| 97 | + |
| 98 | +1. Select **Feature manager** and locate the *Beta* feature flag. Enable the flag by selecting the checkbox under **Enabled**. |
| 99 | + |
| 100 | +1. Wait for a few seconds and you will see the console outputs change. |
| 101 | + |
| 102 | + ``` console |
| 103 | + Beta is enabled: true |
| 104 | + ``` |
0 commit comments