Skip to content

Commit 854d7e0

Browse files
authored
Merge pull request #2 from launchdarkly/mollyjones2723/sc-154156/update-node-js-server-side-sdk-hello-app
sc 154156/update node js server side sdk hello app
2 parents 464c731 + 4f58205 commit 854d7e0

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
### LaunchDarkly Sample TypeScript Node.js Application ###
2-
We've built a simple console application that demonstrates how LaunchDarkly's SDK works. Below, you'll find the basic build procedure, but for more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/).
1+
# LaunchDarkly sample TypeScript Node.js (server-side) application
2+
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).
6+
7+
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.
8+
9+
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).
10+
11+
## Build instructions
312

4-
##### Build instructions #####
513
1. Install the LaunchDarkly Node.js SDK by running `npm install`
6-
2. Copy your API key and feature flag key from your LaunchDarkly dashboard into `index.ts`
7-
3. Run `npm start`
14+
2. Edit `index.ts` 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.
15+
16+
```js
17+
const sdkKey = "1234567890abcdef";
18+
19+
const featureFlagKey = "my-flag";
20+
```
21+
22+
3. Run `npm start`

index.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
import * as LaunchDarkly from 'launchdarkly-node-server-sdk';
22

3-
// TODO : Enter your LaunchDarkly SDK key here
4-
const client = LaunchDarkly.init("YOUR_SDK_KEY");
3+
// Set sdkKey to your LaunchDarkly SDK key.
4+
const sdkKey = "";
55

6+
// Set featureFlagKey to the feature flag key you want to evaluate.
7+
const featureFlagKey = "my-boolean-flag";
8+
9+
// Set up the user properties. This use should appear on your LaunchDarkly
10+
// users dashboard soon after you run the demo.
611
const user = {
7-
"firstName":"Bob",
8-
"lastName":"Loblaw",
9-
"key":"louise@example.com",
10-
"custom":{
11-
"groups":"beta_testers"
12-
}
12+
"name": "Sandy",
13+
"key": "example-user-key"
1314
};
1415

16+
function showMessage(s: string) {
17+
console.log("*** " + s);
18+
console.log("");
19+
}
20+
21+
const client = LaunchDarkly.init(sdkKey);
22+
1523
client.once('ready', function() {
16-
// TODO : Enter the key for your feature flag here
17-
client.variation("enable-pinning", user, false, function(err, showFeature) {
24+
showMessage("SDK successfully initialized!");
25+
client.variation(featureFlagKey, user, false, function(err, showFeature) {
1826
client.track("event-called", user);
1927
if (showFeature) {
2028
// application code to show the feature
21-
console.log("Showing your feature to " + user.key );
29+
showMessage("Feature flag '" + featureFlagKey + "' is true for this user");
2230
} else {
23-
// the code to run if the feature is off
24-
console.log("Not showing your feature to " + user.key);
31+
// the code to run if the feature is off
32+
showMessage("Feature flag '" + featureFlagKey + "' is false for this user");
2533
}
34+
35+
// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
36+
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
37+
// the user properties and flag usage statistics will not appear on your dashboard. In a
38+
// normal long-running application, the SDK would continue running and events would be
39+
// delivered automatically in the background.
2640
client.flush(function() {
2741
client.close();
2842
});

0 commit comments

Comments
 (0)