Skip to content

Commit b8b9e4a

Browse files
committed
initial commit
0 parents  commit b8b9e4a

File tree

8 files changed

+1479
-0
lines changed

8 files changed

+1479
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
10+
[Makefile]
11+
indent_style = tab

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
node_modules
2+
lib
3+
*.log
4+
coverage
5+
dist
6+
out
7+
8+
.DS_Store
9+
10+
npm-debug.log*
11+
12+
.idea
13+
14+
.vscode
15+
16+
.featurevisor

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 100,
3+
"singleQuote": false,
4+
"trailingComma": "all",
5+
"tabWidth": 2,
6+
"semi": true
7+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Fahad Heylaal (https://fahad19.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# featurevisor-example-expressjs
2+
3+
Example of using [Featurevisor](https://featurevisor.com/) with [Express.js](https://expressjs.com/) application.
4+
5+
# Installation
6+
7+
Clone the repository and install dependencies:
8+
9+
```
10+
$ npm ci
11+
```
12+
13+
# Usage
14+
15+
Run the application:
16+
17+
```
18+
$ npm start
19+
```
20+
21+
Visit [http://localhost:3000](http://localhost:3000) to see the application.
22+
23+
# License
24+
25+
MIT © [Fahad Heylaal](https://fahad19.com)

index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const express = require("express");
2+
const { createInstance } = require("@featurevisor/sdk");
3+
4+
require("isomorphic-fetch");
5+
6+
/**
7+
* Constants
8+
*/
9+
const PORT = 3000;
10+
const REFRESH_INTERVAL = 60 * 5; // every 5 minutes
11+
12+
const DATAFILE_URL =
13+
process.env.environment === "production"
14+
? "https://featurevisor-example-cloudflare.pages.dev/production/datafile-tag-all.json"
15+
: "https://featurevisor-example-cloudflare.pages.dev/staging/datafile-tag-all.json";
16+
17+
/**
18+
* Featurevisor instance
19+
*/
20+
const f = createInstance({
21+
datafileUrl: DATAFILE_URL,
22+
23+
onReady: () => console.log(`Featurevisor SDK is now ready`),
24+
onRefresh: () => console.log(`Featurevisor SDK has refreshed`),
25+
onUpdate: () => console.log(`Featurevisor SDK has updates`),
26+
27+
// optionally refresh the datafile every 5 minutes,
28+
// without having to restart the server
29+
refreshInterval: REFRESH_INTERVAL,
30+
});
31+
32+
/**
33+
* Express app with middleware
34+
*/
35+
const app = express();
36+
37+
app.use((req, res, next) => {
38+
req.f = f;
39+
next();
40+
});
41+
42+
/**
43+
* Routes
44+
*/
45+
app.get("/", (req, res) => {
46+
const featureKey = "baz";
47+
const context = { userId: "user-123" };
48+
49+
const isEnabled = f.isEnabled(featureKey, context);
50+
51+
if (isEnabled) {
52+
res.send("Hello World!");
53+
} else {
54+
res.send("Not enabled yet!");
55+
}
56+
});
57+
58+
/**
59+
* Start the server
60+
*/
61+
app.listen(PORT, () => {
62+
console.log(`Server running at http://localhost:${PORT}`);
63+
});

0 commit comments

Comments
 (0)