Demo of OpenFeature usage in an ExpressJS server, in plain JavaScript.
At the time of writing (26th October 2024), OpenFeature's tutorials really push for developers to use some complex cloud-hosted or Docker-managed flag provider service. That's an insane way to try to introduce people to something. Introductory tutorials can't assume knowledge like that!
So, this repo shows one of the simpler ways to use OpenFeature: load up some JSON and parse it.
Server process flow:
- Request comes in.
- Server initialises.
- Server applies a middleware to all routes:
setUpFlagProvider
- Server checks for a flag and wants to make it available to all routes:
checkBananaFlag
- This could instead be applied to a singular router or route, so that flags are only checked on routes that need that flag.
- Flag data is attached to a custom
response.flags
object, but this flag data could be saved to anything.response
is chosen because it'll be available to any other middleware as well as the final callback for the route handler, so the flag data will be available on every bit of that handler chain.
- Middleware chain proceeds along until a route's callback is reached.
- Route callbacks use the checked flags in conditional logic.
To see how this feature flag stuff impacts the server functionality, clone the repo and follow these steps:
- Install project dependencies with
npm install
- Run the server in hot-reload mode with
npm run dev
- Visit
localhost:8080
in your web browser. - Open up the
flags.json
file in the root of this repo. - Modify the
"defaultVariant": "off"
line so thatoff
is replaced withon
. - Refresh the server page in the web browser. The
npm run dev
step should've made the server hot-reload on a change to that flag file, so the response in the browser will be different!
Next steps could go in various directions:
- look into more-complex ways to host the flag data (eg. environment variables using the Environment Variable Provider, or whatever online file storage or database storage you use to simply store some JSON data)
- convert the flag JSON into a JavaScript object, so that more-powerful and dynamic targeting could be implemented to make per-user flags