-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path05_openfeature_with_targeting.js
More file actions
49 lines (42 loc) · 1.1 KB
/
05_openfeature_with_targeting.js
File metadata and controls
49 lines (42 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import express from "express";
import cowsay from "cowsay";
import { OpenFeature, InMemoryProvider } from "@openfeature/server-sdk";
const app = express();
const routes = express.Router();
app.use((_, res, next) => {
res.setHeader("content-type", "text/plain");
next();
}, routes);
const featureFlags = OpenFeature.getClient();
const FLAG_CONFIGURATION = {
'with-cows': {
variants: {
on: true,
off: false
},
disabled: false,
defaultVariant: "off",
contextEvaluator: (context) => {
if (context.cow === "Bessie") {
return "on";
}
return "off";
},
}
};
const featureFlagProvider = new InMemoryProvider(FLAG_CONFIGURATION);
OpenFeature.setProvider(featureFlagProvider);
routes.get("/", async (req, res) => {
const context = {
cow: req.get("x-cow")
};
const withCows = await featureFlags.getBooleanValue("with-cows", false, context);
if (withCows) {
res.send(cowsay.say({ text: "Hello, world!" }));
} else {
res.send("Hello, world!");
}
});
app.listen(3333, () => {
console.log("Server running at http://localhost:3333");
});