|
| 1 | +--- |
| 2 | +title: Quickstart for using Azure App Configuration with JavaScript apps |
| 3 | +description: In this quickstart, create a Node.js app with Azure App Configuration to centralize storage and management of application settings separate from your code. |
| 4 | +services: azure-app-configuration |
| 5 | +author: eskibear |
| 6 | +ms.service: azure-app-configuration |
| 7 | +ms.devlang: javascript |
| 8 | +ms.topic: quickstart |
| 9 | +ms.custom: quickstart, mode-other, devx-track-js |
| 10 | +ms.date: 10/12/2023 |
| 11 | +ms.author: yanzh |
| 12 | +#Customer intent: As a JavaScript developer, I want to manage all my app settings in one place. |
| 13 | +--- |
| 14 | +# Quickstart: Create a JavaScript app with Azure App Configuration |
| 15 | + |
| 16 | +In this quickstart, you'll use Azure App Configuration to centralize storage and management of application settings using the [Azure App Configuration JavaScript provider client library](https://github.com/Azure/AppConfiguration-JavaScriptProvider). |
| 17 | + |
| 18 | +App Configuration provider for JavaScript is built on top of the [Azure SDK for JavaScript](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/appconfiguration/app-configuration) and is designed to be easier to use with richer features. |
| 19 | +It enables access to key-values in App Configuration as a `Map` object. |
| 20 | +It offers features like configuration composition from multiple labels, key prefix trimming, automatic resolution of Key Vault references, and many more. |
| 21 | +As an example, this tutorial shows how to use the JavaScript provider in a Node.js app. |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/). |
| 26 | +- An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store). |
| 27 | +- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule). For information about installing Node.js either directly on Windows or using the Windows Subsystem for Linux (WSL), see [Get started with Node.js](/windows/dev-environment/javascript/nodejs-overview) |
| 28 | + |
| 29 | +## Add key-values |
| 30 | + |
| 31 | +Add the following key-values to the App Configuration store. For more information about how to add key-values to a store using the Azure portal or the CLI, go to [Create a key-value](./quickstart-azure-app-configuration-create.md#create-a-key-value). |
| 32 | + |
| 33 | +| Key | Value | Content type | |
| 34 | +|----------------|----------------------------------------|--------------------| |
| 35 | +| *message* | *Message from Azure App Configuration* | Leave empty | |
| 36 | +| *app.greeting* | *Hello World* | Leave empty | |
| 37 | +| *app.json* | *{"myKey":"myValue"}* | *application/json* | |
| 38 | + |
| 39 | +## Setting up the Node.js app |
| 40 | + |
| 41 | +In this tutorial, you'll create a Node.js console app and load data from your App Configuration store. |
| 42 | + |
| 43 | +1. Create a new directory for the project named *app-configuration-quickstart*. |
| 44 | + |
| 45 | + ```console |
| 46 | + mkdir app-configuration-quickstart |
| 47 | + ``` |
| 48 | + |
| 49 | +1. Switch to the newly created *app-configuration-quickstart* directory. |
| 50 | + |
| 51 | + ```console |
| 52 | + cd app-configuration-quickstart |
| 53 | + ``` |
| 54 | + |
| 55 | +1. Install the Azure App Configuration provider by using the `npm install` command. |
| 56 | + |
| 57 | + ```console |
| 58 | + npm install @azure/app-configuration-provider |
| 59 | + ``` |
| 60 | + |
| 61 | +1. Create a new file called *app.js* in the *app-configuration-quickstart* directory and add the following code: |
| 62 | + |
| 63 | + ```javascript |
| 64 | + const { load } = require("@azure/app-configuration-provider"); |
| 65 | + const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING; |
| 66 | + |
| 67 | + async function run() { |
| 68 | + let settings; |
| 69 | + |
| 70 | + // Sample 1: Connect to Azure App Configuration using a connection string and load all key-values with null label. |
| 71 | + settings = await load(connectionString); |
| 72 | + |
| 73 | + // Find the key "message" and print its value. |
| 74 | + console.log(settings.get("message")); // Output: Message from Azure App Configuration |
| 75 | + |
| 76 | + // Find the key "app.json" as an object, and print its property "myKey". |
| 77 | + const jsonObject = settings.get("app.json"); |
| 78 | + console.log(jsonObject.myKey); // Output: myValue |
| 79 | + |
| 80 | + // Sample 2: Load all key-values with null label and trim "app." prefix from all keys. |
| 81 | + settings = await load(connectionString, { |
| 82 | + trimKeyPrefixes: ["app."] |
| 83 | + }); |
| 84 | + |
| 85 | + // From the keys with trimmed prefixes, find a key with "greeting" and print its value. |
| 86 | + console.log(settings.get("greeting")); // Output: Hello World |
| 87 | + |
| 88 | + // Sample 3: Load all keys starting with "app." prefix and null label. |
| 89 | + settings = await load(connectionString, { |
| 90 | + selectors: [{ |
| 91 | + keyFilter: "app.*" |
| 92 | + }], |
| 93 | + }); |
| 94 | + |
| 95 | + // Print true or false indicating whether a setting is loaded. |
| 96 | + console.log(settings.has("message")); // Output: false |
| 97 | + console.log(settings.has("app.greeting")); // Output: true |
| 98 | + console.log(settings.has("app.json")); // Output: true |
| 99 | + } |
| 100 | + |
| 101 | + run().catch(console.error); |
| 102 | + ``` |
| 103 | + |
| 104 | +## Run the application locally |
| 105 | + |
| 106 | +1. Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING**, and set it to the connection string of your App Configuration store. At the command line, run the following command: |
| 107 | + |
| 108 | + ### [Windows command prompt](#tab/windowscommandprompt) |
| 109 | + |
| 110 | + To run the app locally using the Windows command prompt, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 111 | + |
| 112 | + ```cmd |
| 113 | + setx AZURE_APPCONFIG_CONNECTION_STRING "<app-configuration-store-connection-string>" |
| 114 | + ``` |
| 115 | + |
| 116 | + ### [PowerShell](#tab/powershell) |
| 117 | + |
| 118 | + If you use Windows PowerShell, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 119 | + |
| 120 | + ```azurepowershell |
| 121 | + $Env:AZURE_APPCONFIG_CONNECTION_STRING = "<app-configuration-store-connection-string>" |
| 122 | + ``` |
| 123 | + |
| 124 | + ### [macOS](#tab/unix) |
| 125 | + |
| 126 | + If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 127 | + |
| 128 | + ```console |
| 129 | + export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>' |
| 130 | + ``` |
| 131 | + |
| 132 | + ### [Linux](#tab/linux) |
| 133 | + |
| 134 | + If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 135 | + |
| 136 | + ```console |
| 137 | + export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>' |
| 138 | + ``` |
| 139 | + |
| 140 | +1. Print the value of the environment variable to validate that it's set properly with the command below. |
| 141 | + |
| 142 | + ### [Windows command prompt](#tab/windowscommandprompt) |
| 143 | + |
| 144 | + Using the Windows command prompt, restart the command prompt to allow the change to take effect and run the following command: |
| 145 | + |
| 146 | + ```cmd |
| 147 | + echo %AZURE_APPCONFIG_CONNECTION_STRING% |
| 148 | + ``` |
| 149 | +
|
| 150 | + ### [PowerShell](#tab/powershell) |
| 151 | +
|
| 152 | + If you use Windows PowerShell, run the following command: |
| 153 | +
|
| 154 | + ```azurepowershell |
| 155 | + $Env:AZURE_APPCONFIG_CONNECTION_STRING |
| 156 | + ``` |
| 157 | +
|
| 158 | + ### [macOS](#tab/unix) |
| 159 | +
|
| 160 | + If you use macOS, run the following command: |
| 161 | +
|
| 162 | + ```console |
| 163 | + echo "$AZURE_APPCONFIG_CONNECTION_STRING" |
| 164 | + ``` |
| 165 | +
|
| 166 | + ### [Linux](#tab/linux) |
| 167 | +
|
| 168 | + If you use Linux, run the following command: |
| 169 | +
|
| 170 | + ```console |
| 171 | + echo "$AZURE_APPCONFIG_CONNECTION_STRING" |
| 172 | + ``` |
| 173 | +
|
| 174 | +1. After the environment variable is properly set, run the following command to run the app locally: |
| 175 | +
|
| 176 | + ```bash |
| 177 | + node app.js |
| 178 | + ``` |
| 179 | +
|
| 180 | + You should see the following output: |
| 181 | +
|
| 182 | + ```Output |
| 183 | + Message from Azure App Configuration |
| 184 | + myValue |
| 185 | + Hello World |
| 186 | + false |
| 187 | + true |
| 188 | + true |
| 189 | + ``` |
| 190 | +
|
| 191 | +## Clean up resources |
| 192 | +
|
| 193 | +[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)] |
| 194 | +
|
| 195 | +## Next steps |
| 196 | +
|
| 197 | +In this quickstart, you created a new App Configuration store and learned how to access key-values using the App Configuration JavaScript provider in a Node.js app. |
| 198 | +
|
| 199 | +For more code samples, visit: |
| 200 | +
|
| 201 | +> [!div class="nextstepaction"] |
| 202 | +> [Azure App Configuration JavaScript provider](https://github.com/Azure/AppConfiguration-JavaScriptProvider/tree/main/examples) |
0 commit comments