You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Examples for Microsoft Feature Management for JavaScript
2
+
3
+
These examples show how to use the Microsoft Feature Management in an express application.
4
+
5
+
## Prerequisites
6
+
7
+
The examples are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).
8
+
9
+
## Setup & Run
10
+
11
+
1. Go to `src/feature-management` under the root folder and run:
12
+
13
+
```bash
14
+
npm run install
15
+
npm run build
16
+
```
17
+
18
+
1. Go back to `examples/express-app` and install the dependencies using `npm`:
19
+
20
+
```bash
21
+
npm install
22
+
```
23
+
24
+
1. Run the examples:
25
+
26
+
```bash
27
+
node server.mjs
28
+
```
29
+
30
+
1. Visit `http://localhost:3000/Beta` and use `userId` and `groups` query to specify the targeting context (e.g. /Beta?userId=Jeff or /Beta?groups=Admin).
31
+
32
+
- If you are not targeted, you will get the message "Page not found".
33
+
34
+
- If you are targeted, you will get the message "Welcome to the Beta page!".
35
+
36
+
## Targeting
37
+
38
+
The targeting mechanism uses the `exampleTargetingContextAccessor` to extract the targeting context from the request. This functionretrieves the userId and groups from the query parameters of the request.
39
+
40
+
```javascript
41
+
const exampleTargetingContextAccessor = {
42
+
getTargetingContext: () => {
43
+
const req = requestAccessor.getStore();
44
+
// read user and groups from request query data
45
+
const { userId, groups } = req.query;
46
+
// return aa ITargetingContext with the appropriate user info
This allows you to get ambient targeting context while doing feature flag evaluation.
64
+
65
+
### Request Accessor
66
+
67
+
The `requestAccessor` is an instance of `AsyncLocalStorage` from the `async_hooks` module. It is used to store the request object in asynchronous local storage, allowing it to be accessed throughout the lifetime of the request. This is particularly useful foraccessing request-specific datain asynchronous operations. For more information, please go to https://nodejs.org/api/async_context.html
68
+
69
+
```javascript
70
+
import { AsyncLocalStorage } from "async_hooks";
71
+
const requestAccessor = new AsyncLocalStorage();
72
+
```
73
+
74
+
Middleware is used to store the request object in the AsyncLocalStorage:
"description": "Feature Management Application Insights Plugin for Browser provides a solution for sending feature flag evaluation events produced by the Feature Management library.",
"description": "Feature Management Application Insights Plugin for Node.js provides a solution for sending feature flag evaluation events produced by the Feature Management library.",
0 commit comments