Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Continuous Integration

on: [push, pull_request]

permissions:
contents: read

jobs:
continuous-integration:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v7

- uses: actions/setup-node@v7
with:
node-version-file: .nvmrc
cache: npm

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm test
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ Example of using [Featurevisor](https://featurevisor.com/) with [Express.js](htt

Clone the repository and install dependencies:

```
$ npm ci
```sh
npm ci
```

## Usage

Run the application:

```
$ npm start
```sh
npm start
```

Visit [http://localhost:3000](http://localhost:3000) to see the application.

The example uses the Featurevisor v3 `createFeaturevisor` API and loads the
published Cloudflare example datafile before starting Express.

Run the automated test with:

```sh
npm test
```

## License

MIT © [Fahad Heylaal](https://fahad19.com)
52 changes: 28 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const express = require("express");
const { createInstance } = require("@featurevisor/sdk");

require("isomorphic-fetch");
const { createFeaturevisor } = require("@featurevisor/sdk");

/**
* Constants
Expand All @@ -16,7 +14,7 @@ const DATAFILE_URL =
/**
* Featurevisor instance
*/
const f = createInstance({});
const f = createFeaturevisor({});

/**
* Express app with middleware
Expand All @@ -28,36 +26,42 @@ app.use((req, res, next) => {
next();
});

/**
* Routes
*/
app.get("/", (req, res) => {
const { f } = req;

function evaluateMessage(instance = f) {
const featureKey = "baz";
const context = { userId: "user-123" };

const isEnabled = f.isEnabled(featureKey, context);
return instance.isEnabled(featureKey, context)
? "Hello World!"
: "Not enabled yet!";
}

if (isEnabled) {
res.send("Hello World!");
} else {
res.send("Not enabled yet!");
}
/**
* Routes
*/
app.get("/", (req, res) => {
res.send(evaluateMessage(req.f));
});

/**
* Start the server
*/
fetch(DATAFILE_URL)
.then((response) => response.json())
.then((datafile) => {
f.setDatafile(datafile);
async function start() {
try {
const response = await fetch(DATAFILE_URL);
const datafile = await response.json();
f.setDatafile(datafile, true);

app.listen(PORT, () => {
return app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
})
.catch((error) => {
} catch (error) {
console.error("Error fetching datafile:", error);
});
process.exitCode = 1;
}
}

if (require.main === module) {
start();
}

module.exports = { app, evaluateMessage, start };
Loading