Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/express-app/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"feature_management": {
"feature_flags": [
{
"id": "Beta",
"enabled": true,
"conditions": {
"client_filters": [
{
"name": "Microsoft.Targeting",
"parameters": {
"Audience": {
"Users": [
"Jeff"
],
"Groups": [
{
"Name": "Admin",
"RolloutPercentage": 100
}
],
"DefaultRolloutPercentage": 40
}
}
}
]
}
}
]
}
}
10 changes: 10 additions & 0 deletions examples/express-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scripts": {
"start": "node server.mjs"
},
"dependencies": {
"@azure/app-configuration-provider": "latest",
"@microsoft/feature-management": "latest",
"express": "^4.21.2"
}
}
64 changes: 64 additions & 0 deletions examples/express-app/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import express from "express";
import path from "path";

import { load } from "@azure/app-configuration-provider";
const connectionString = "<your-connection-string>";;
const appConfig = await load(connectionString, {
featureFlagOptions: {
enabled: true,
selectors: [{
keyFilter: "*"
}],
refresh: {
enabled: true
}
}
});

appConfig.onRefresh(() => {
console.log("Configuration has been refreshed.");
});

import { ConfigurationObjectFeatureFlagProvider, ConfigurationMapFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management";

/*
You can use either ConfigurationObjectFeatureFlagProvider or ConfigurationMapFeatureFlagProvider to provide feature flags.
We recommend using Azure App Configuration as the source of feature flags.
*/
// const config = JSON.parse(await fs.readFile("config.json"));
// const featureProvider = new ConfigurationObjectFeatureFlagProvider(config);

const featureProvider = new ConfigurationMapFeatureFlagProvider(appConfig);
const featureManager = new FeatureManager(featureProvider);

const server = express();
const PORT = 3000;

// Use a middleware to achieve request-driven configuration refresh
server.use((req, res, next) => {
// this call s not blocking, the configuration will be updated asynchronously
appConfig.refresh();
next();
})


server.get("/", (req, res) => {
appConfig.refresh();
res.send("Hello World!");
});

server.get("/Beta", async (req, res) => {
appConfig.refresh();
const { userId, groups } = req.query;

if (await featureManager.isEnabled("Beta", { userId: userId, groups: groups ? groups.split(",") : [] })) {
res.send("Welcome to the Beta page!");
} else {
res.status(404).send("Page not found");
}
});

// Start the server
server.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});