Skip to content

Commit 8d5b692

Browse files
Merge pull request #14 from launchdarkly/lc/update-based-on-spec
fix: Update hello app and README according to new spec. Add CODEOWNERS
2 parents 58ef89f + 6883d7d commit 8d5b692

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Repository Maintainers
2+
* @launchdarkly/team-sdk-js

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
# LaunchDarkly sample Node.js (server-side) application
22

3-
We've built a simple console application that demonstrates how LaunchDarkly's SDK works. 
Below, you'll find the build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [Node.js (server-side) reference guide](https://docs.launchdarkly.com/sdk/server-side/node-js).
3+
We've built a simple console application that demonstrates how LaunchDarkly's SDK works.
4+
5+
Below, you'll find the build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [Node.js (server-side) reference guide](https://docs.launchdarkly.com/sdk/server-side/node-js).
46

57
The LaunchDarkly server-side SDK for Node.js is designed primarily for use in multi-user systems such as web servers and applications. It follows the server-side LaunchDarkly model for multi-user contexts. It is not intended for use in desktop and embedded systems applications.
68

79
For a sample application demonstrating how to use LaunchDarkly in *client-side* Node.js applications, refer to our [Client-side Node.js SDK sample application](https://github.com/launchdarkly/hello-node-client).
810

911
## Build instructions
1012

11-
1. Install the LaunchDarkly Node.js SDK by running `npm install`
12-
13-
2. Edit `index.js` and set the value of `sdkKey` to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `featureFlagKey` to the flag key.
13+
1. Set the environment variable `LAUNCHDARKLY_SERVER_KEY` to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `LAUNCHDARKLY_FLAG_KEY` to the flag key; otherwise, a boolean flag of `sample-feature` will be assumed.
1414

15-
```js
16-
const sdkKey = "1234567890abcdef";
15+
```bash
16+
export LAUNCHDARKLY_SERVER_KEY="1234567890abcdef"
17+
export LAUNCHDARKLY_FLAG_KEY="my-boolean-flag"
18+
```
1719

18-
const featureFlagKey = "my-flag";
19-
```
20+
2. Install the LaunchDarkly Node.js SDK by running `npm install`
2021

2122
3. Run `node index.js`
2223

23-
You should receive the message `"Feature flag '<flag key>' is <true/false> for this user"`.
24+
You should receive the message "The <flagKey> feature flag evaluates to <flagValue>.". The application will run continuously and react to the flag changes in LaunchDarkly.

index.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
1-
var LaunchDarkly = require('@launchdarkly/node-server-sdk');
1+
const LaunchDarkly = require('@launchdarkly/node-server-sdk');
22

33
// Set sdkKey to your LaunchDarkly SDK key.
4-
const sdkKey = "";
4+
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY ?? 'your-sdk-key';
55

66
// Set featureFlagKey to the feature flag key you want to evaluate.
7-
const featureFlagKey = "my-boolean-flag";
7+
const featureFlagKey = process.env.LAUNCHDARKLY_FLAG_KEY ?? 'sample-feature';
88

9-
function showMessage(s) {
10-
console.log("*** " + s);
11-
console.log("");
9+
function showBanner() {
10+
console.log(
11+
` ██
12+
██
13+
████████
14+
███████
15+
██ LAUNCHDARKLY █
16+
███████
17+
████████
18+
██
19+
██
20+
`,
21+
);
1222
}
1323

14-
if (sdkKey == "") {
15-
showMessage("Please edit index.js to set sdkKey to your LaunchDarkly SDK key first");
24+
function printValueAndBanner(flagValue) {
25+
console.log(`*** The '${featureFlagKey}' feature flag evaluates to ${flagValue}.`);
26+
27+
if (flagValue) showBanner();
28+
}
29+
30+
if (!sdkKey) {
31+
console.log('*** Please edit index.js to set sdkKey to your LaunchDarkly SDK key first.');
1632
process.exit(1);
1733
}
1834

@@ -21,26 +37,30 @@ const ldClient = LaunchDarkly.init(sdkKey);
2137
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
2238
// soon after you run the demo.
2339
const context = {
24-
kind: "user",
25-
key: "example-context-key",
26-
name: "Sandy"
40+
kind: 'user',
41+
key: 'example-user-key',
42+
name: 'Sandy',
2743
};
2844

29-
ldClient.waitForInitialization().then(function () {
30-
showMessage("SDK successfully initialized!");
31-
ldClient.variation(featureFlagKey, context, false, function (err, flagValue) {
32-
showMessage("Feature flag '" + featureFlagKey + "' is " + flagValue + " for this context");
33-
34-
// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
35-
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
36-
// the context properties and flag usage statistics will not appear on your dashboard. In a
37-
// normal long-running application, the SDK would continue running and events would be
38-
// delivered automatically in the background.
39-
ldClient.flush(function () {
40-
ldClient.close();
45+
ldClient
46+
.waitForInitialization()
47+
.then(() => {
48+
console.log('*** SDK successfully initialized!');
49+
50+
const eventKey = `update:${featureFlagKey}`;
51+
ldClient.on(eventKey, () => {
52+
ldClient.variation(featureFlagKey, context, false).then(printValueAndBanner);
4153
});
54+
55+
ldClient.variation(featureFlagKey, context, false).then((flagValue) => {
56+
printValueAndBanner(flagValue);
57+
58+
if(typeof process.env.CI !== "undefined") {
59+
process.exit(0);
60+
}
61+
});
62+
})
63+
.catch((error) => {
64+
console.log(`*** SDK failed to initialize: ${error}`);
65+
process.exit(1);
4266
});
43-
}).catch(function (error) {
44-
showMessage("SDK failed to initialize: " + error);
45-
process.exit(1);
46-
});

0 commit comments

Comments
 (0)