Demo: A simple Node.js app with Feature Flags

Wed Sep 01 2021

Using Feature Flags/Gates in your app is simple and straightforward. This article will show you a step-by-step process of creating a simple Node.js app that responds to feature gate changes in almost-real-time.

This article assumes you haveĀ npmĀ installed on your dev environment. More detailedĀ documentation is available here.

If you donā€™t already have an account, create one for free atĀ https://www.statsig.com/Ā and create a new Feature Gate named ā€œFirst AppĀ Testā€

creating a feature gate example

The first step to creating the Node app in your command line terminalĀ is:

$ mkdir statsigNodeApp && cd statsigNodeApp $ npm init --yes

This will create aĀ package.jsonĀ file in theĀ statsigNodeAppĀ directory.

Next, letā€™s install statsig-node SDK.

$ npm install statsig-node

Now, weā€™re all set to write our main appā€™s logic. Hereā€™s the code that checks the gate we just created every second and writes out whether it passes or not. Copy and paste this into a file namedĀ index.js.

https://medium.com/media/153cc5e35683db3d164c4e419232f32b/href

One last thing is you need to replace the ā€˜secret-API-KEYā€™ with your own API key, which you can find in your projectā€™s settings:

running the feature gate example

With that replaced, you are ready to run this app by issuing thisĀ command:

$ node index.js

You will see something like this in theĀ output:

node index failed example

This means that the flag is evaluating to ā€˜falseā€™ all the time. Letā€™s go ahead and create a new rule that turns on this feature for everyone.

adding a new rule

And letā€™s make sure to hit that ā€˜Save Changesā€™Ā button.

make sure to hit the save changes button in statsig lol

Now, if we had kept that Node App running, youā€™ll notice that it has started saying ā€˜passedā€™ automatically, since now this feature flag is turned on to everyone.

automatic passing in node

You can now try turning off this feature in theĀ console.

turning on the feature in the console

Which will immediately make the feature fail the check and the app will go from ā€˜passedā€™ to ā€˜failedā€™ to reflect theĀ change.

pass to failed in the node console

And thatā€™sĀ it!

Hope this was helpful in getting you started and on your way to creating more complex features! As always weā€™re here to help out. You can join our Slack community here:Ā https://statsig.com/community.

Build fast?

Subscribe to Scaling Down: Our newsletter on building at startup-speed.

Try Statsig Today

Get started for free. Add your whole team!
We use cookies to ensure you get the best experience on our website.
Privacy Policy