Leveraging a Spring Cloud Config Server in a Node.js App's Feature Toggles (Pt 2)
Tags: javascript, node, devops, springboot
Published: 2022-05-15 13:42:35

---
title: Leveraging a Spring Cloud Config Server in a Node.js App's Feature Toggles (Pt 2)
published: true
date: 2018-06-02 00:00:00 UTC
tags: javascript,nodejs,devops,springboot
canonical_url: https://www.paigeniedringhaus.com/blog/leveraging-a-spring-cloud-config-server-in-a-node-js-apps-feature-toggles-pt-2
---

[![Laptop screen filled with minified source code](https://www.paigeniedringhaus.com/static/1bc85da9de192091881a510c69f4a2e3/15ec7/more-code.jpg "Laptop screen filled with minified source code")](/static/1bc85da9de192091881a510c69f4a2e3/eea4a/more-code.jpg)

## Introduction

As I showed in my [first post about the Spring Cloud Config server](https://www.paigeniedringhaus.com/blog/why-a-spring-cloud-config-server-is-crucial-to-a-good-ci-cd-pipeline-pt-1), setting up a configuration server for a Java Spring Boot application and then leveraging that server to provide environment variables to existing Spring Boot projects is really handy.

The next trick was figuring out if it was possible to use that very same config server in a Node,js project.

To give you a quick rundown, my team worked on a microservice-based application architecture, meaning, we had up to thirteen separate Java services and one Node service all running to power our application.

It made total sense to go with a Java-based config server, since: thirteen Spring Boot microservices, but when our business partners decided they wanted to implement feature toggles, it became a priority to get the config server working in the Node project as well.

---

## What is a Feature Toggle, and Why Do I Need It?

Let me set the stage by giving you a quick run down of what a feature toggle is and why it’s so beneficial for developers, business partners and customers.

> A **feature toggle** is a technique in [software development](https://en.wikipedia.org/wiki/Software_development) that attempts to provide an alternative to maintaining multiple [source-code](https://en.wikipedia.org/wiki/Source_code) branches (known as feature branches), such that a feature can be tested even before it is completed and ready for release. A feature toggle is used to hide, enable or disable the feature during run time. For example, during the development process, a developer can enable the feature for testing and disable it for other users.
> 
> [— Wikipedia, Feature Toggle](https://en.wikipedia.org/wiki/Feature_toggle)

Basically, a feature toggle allows developers to introduce new features to users in a more Agile development fashion.

With feature toggles, devs can continue to maintain just one codebase but show different views of the UI to users in production versus the development team in lower life cycles, based on if those toggles are set to ‘on’ or ‘off’.

They also allow our business and UX partners to validate early on, if new features are beneficial to our users, or if they need to reevaluate how we’re approaching a solution.

Let me make one thing crystal clear though: **feature toggles are not meant to be permanent solutions.** They are temporary, meant to remain only until the feature has been completed and validated that it’s useful or meets whatever other criteria was set for success or failure. Then the code around the feature toggle should be removed to keep the codebase clean and as free of technical debt as is possible.

For my specific situation, our feature toggle was needed to hide a button that didn’t have full functionality yet because the backend service to support it wasn’t finished, and hide the radio buttons from users. They wouldn’t be needed until the button and service were done.

[![The UI a typical user sees (feature toggle hides buttons and checkboxes)](https://www.paigeniedringhaus.com/static/097dd5b077051b3987a8884e99515af7/1e043/feature-toggle-users.png "The UI a typical user sees (feature toggle hides buttons and checkboxes)")](/static/097dd5b077051b3987a8884e99515af7/2cefc/feature-toggle-users.png)

_Feature toggle view for users: no button and no checkboxes._

Above is the UI view users in production would see: no buttons, no checkboxes. Below is the UI view developers working on the feature need to see: buttons and checkboxes present.

[![The UI a developer sees (feature toggle shows buttons and checkboxes)](https://www.paigeniedringhaus.com/static/cd8585f87c6ca9fa4882efabf18b3f41/1e043/feature-toggle-devs.png "The UI a developer sees (feature toggle shows buttons and checkboxes)")](/static/cd8585f87c6ca9fa4882efabf18b3f41/2cefc/feature-toggle-devs.png)

_Feature toggle view for devs: buttons and checkboxes._

Let's look at how we got these two views simultaneously.

---

## Setting Up Your Config Server Properties

Before we get to the Node config server, let’s first set up the new file with the following naming conventions in our config server properties repo:

```yaml
[application name]-[life cycle profile].yml
ex. my-ui-app-to-config-QA.yml
```

Within this YAML file, you’ll be able to add your configuration properties for your UI feature toggle. Here’s all I had to include for my feature toggles.

```yaml
modifyDatesToggle: true
```

Commit this to a Github repo, and we’re set there. You can see an example of the config properties repo [here in Github](https://github.com/paigen11/spring-cloud-config-properties-sample).

### nodecloud-config-client Node.js Set Up

To leverage the existing Spring Cloud Config server set up with our Node frontend application, I chose the [`nodecloud-config-client`](https://www.npmjs.com/package/nodecloud-config-client) on npm, as it’s a fairly well documented Spring Cloud Config Server client written for JavaScript.

> I should mention, this is not to be confused with `node-cloud-config-client` or `cloud-config-client` — really, there’s a million versions of config client packages written for Node.js, but the one listed above and in the resources link is the one I used.

So to get down to business, after saving the npm package to our UI’s `package.json` dependencies, add it to the `server.js`file (or wherever your Node server’s configuration is located).

[![Code set up in app.js file showing the cloud-config-client requirement](https://www.paigeniedringhaus.com/static/3e4d6fedebe868cce1a95acbd2414d9e/1e043/node-cloud-config.png "Code set up in app.js file showing the cloud-config-client requirement")](/static/3e4d6fedebe868cce1a95acbd2414d9e/1e043/node-cloud-config.png)

_See the `client` variable at the bottom — that’s where the config client comes in to play._

If you’re developing in multiple environments like my team did (local, QA, Q1, production, etc.), add a variable to access the Spring Cloud config server for all stages of the development process (these [environment variable services](https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES) are how we connected to it in Pivotal Cloud Foundry). I mentioned these back in my **[previous post](https://www.paigeniedringhaus.com/blog/why-a-spring-cloud-config-server-is-crucial-to-a-good-ci-cd-pipeline-pt-1)**, if you’d like more info.

[![Logic in Node server determining how to fetch feature toggles from the Spring Cloud Config server](https://www.paigeniedringhaus.com/static/8b2f4460d32b995fd95fc968a5a96c72/1e043/config-logic.png "Logic in Node server determining how to fetch feature toggles from the Spring Cloud Config server")](/static/8b2f4460d32b995fd95fc968a5a96c72/610c0/config-logic.png)

_The `configServerUrl` goes through a series of checks for environment variables, and if it finds none of those (like during local development), it defaults to the hard coded URL. I used the same environment variables and logic for the `configServicesProfile` and `featureToggle`._

Next, add a variable to account for where the config services profile will be located (for local development I had it default to QA). And I added a variable to check for enabled/disabled feature toggles.

The below code connects to the config server (when feature toggles are enabled), and maps any feature toggles found to an object.

```javascript
{ modifyDatesFeature : true }
```

Then that object is sent to the rest of the UI with the `app.get` endpoint simply called `"/featureToggles"`.

[![Node code connecting to config server and checking for feature toggles](https://www.paigeniedringhaus.com/static/ec2063dabcc7ff699489c64a0bf017ca/1e043/node-feature-toggle.png "Node code connecting to config server and checking for feature toggles")](/static/ec2063dabcc7ff699489c64a0bf017ca/62a6a/node-feature-toggle.png)

_Feature flag variable, call to the client to access the config server with the correct environment (QA in my case), and a check if the `featureToggle` is `‘on’`, map the properties that are available to it. Below is the endpoint to call the features._

Great. The Node server is set up, and you can get the features toggles (or whatever else you’re storing in the config server) just by changing a few variables.

### Connecting Your Client Side UI Framework to Your Node Server

The first thing you’ll need to do on the frontend is make an AJAX call to the Node server to check for any existing feature toggles. The framework of the app implementing this was JavaScript MVC (an older framework popular years ago, I'd never heard of it before I worked with it either), and here’s an example of what the call looked like.

[![JavaScript client-side code calling for any feature toggles from Node server](https://www.paigeniedringhaus.com/static/08fa392466f831d1e687036a78b88e80/1e043/javascript-mvc.png "JavaScript client-side code calling for any feature toggles from Node server")](/static/08fa392466f831d1e687036a78b88e80/000c7/javascript-mvc.png)

In the JavaScript file that’s actually concerned with feature flags (for me, it was a `dates.js` file), I imported the feature toggle AJAX call and created a function to check for the specific feature flag associated with this functionality: `modifyDatesToggle`.

[![Feature toggle code for showing / hiding particular date / time feature](https://www.paigeniedringhaus.com/static/c9c250b288bb39d8f8c12f1643ce353f/1e043/js-feature-toggle-code.png "Feature toggle code for showing / hiding particular date / time feature")](/static/c9c250b288bb39d8f8c12f1643ce353f/5f6dd/js-feature-toggle-code.png)

_The feature flag was imported at the top of the file, then I call to the feature flag endpoint to see if there’s anything there the file needs to be aware of. If the feature flag matches the variable I named “modifyDatesToggle”, it gets pulled in and applied to the file._

Finally, inside your JavaScript file using the feature toggle, group the code, wherever possible according to if the feature toggle is enabled, like so.

[![Code modifications that happen when feature toggle is enabled](https://www.paigeniedringhaus.com/static/f74ee8cfe78d761e09949ec07632fb67/1e043/feature-toggle-grouped-code.png "Code modifications that happen when feature toggle is enabled")](/static/f74ee8cfe78d761e09949ec07632fb67/3c492/feature-toggle-grouped-code.png)

_Example 1: This check displays the check boxes if the `modifyDatesToggle` is `“on.”`_

The thinking goes, that by wrapping all the feature enabled code inside of the feature toggle check, even if the call to the config server fails for some reason, users will only ever see the code that should be deployed to production. This gives us extra protection by not being as dependent on more variables than necessary.

---

## But Wait, There’s More — Enabling End-to-End Tests with Feature Toggles

There’s one more thing I wanted to include in this blog: how I used the same feature toggle logic to run or skip some of the Protractor end-to-end tests.

If you’re using a configuration file for Protractor, and I don’t know why anyone wouldn’t, there’s a cool feature that lets you make your own [custom parameters](https://moduscreate.com/blog/protractor_parameters_adding_flexibility_automation_tests/). I took a page out of my own book where the feature toggle is set in the `server.js` file, and did a similar set up in the Protractor config file: if environment variables are passed in for the feature toggle, the tests that concern that feature will run, if it’s not, they’ll be ignored — I’ll show you the test syntax in a moment, as it’s a little unconventional.

[![Feature toggle setup in e2e testing configuration](https://www.paigeniedringhaus.com/static/16ebbe609d61f226bf2d751080c234ba/1e043/feature-toggle-e2e-config.png "Feature toggle setup in e2e testing configuration")](/static/16ebbe609d61f226bf2d751080c234ba/63ec5/feature-toggle-e2e-config.png)

_Here’s the configuration that all the Protractor tests use. Params takes in the modifyDatesToggle object and if it exists, it sets the params, if it doesn’t, it sets it to null._

When you’re running the tests locally, you can actually pass the params in through the command line like this:

``bash
$ protractor e2e/conf.js browser.params.modifyDatesToggle=‘true’
```

To implement the feature toggle in the actual end-to-end tests, inside of each test that requires a feature toggle check, add an if statement right after the `‘it’` declaration, checking if the feature toggle is null or not. As I said, this is a little weird to see, but it works.

[![Automated test utilizing feature toggled code](https://www.paigeniedringhaus.com/static/5b10677f0d6acfc3724bddbf96a836d1/1e043/feature-toggle-test.png "Automated test utilizing feature toggled code")](/static/5b10677f0d6acfc3724bddbf96a836d1/a3767/feature-toggle-test.png)

_An example of a Protractor end-to-end test with a feature toggle check. Not all the tests need this check — just the ones that require the feature toggle to be either off or on._

If the feature toggle is not enabled, the test will be skipped, but show up as if it passed in the console (eliminating failing tests because the proper environment wasn’t present for the test to use).

If the feature toggle is enabled, the test will run as normal. This prevents us from having to go through the tests one by one and either `x` them out to ignore them or remove the `x` so they’ll run depending on what the desired functionality is. And once it’s time to remove the feature toggle, it’s easy enough to either remove the unnecessary tests from the `spec` file or just `x` them out, depending on your team’s preference for keeping tests even after functionality has changed. (Personally, I would remove it.)

Above, I showed how to run the feature toggle tests from the command line, but it can also be run programmatically when the application is being deployed.

Segue: for my team personally, we use [Jenkins](https://jenkins.io/) for all our builds. That’s a whole other set of blog posts, but suffice it to say, while the build is running all the unit test are run, all the end-to-end tests are running, all the rest of the checks we have in place before declaring a feature is tested and ready for our business team to accept happens.

Back to how this matters: since Jenkins is our job runner, we can pass the environment variables for the feature toggle tests to Jenkins through a `Jenkinsfile`.

[![Enabling feature toggle in Jenkinsfile](https://www.paigeniedringhaus.com/static/2aa4b21d04f265f1c5152f0faf43d15f/1e043/feature-toggles-jenkinsfile.png "Enabling feature toggle in Jenkinsfile")](/static/2aa4b21d04f265f1c5152f0faf43d15f/8cdda/feature-toggles-jenkinsfile.png)

_All that’s needed is the simple line at the bottom: `env.MODIFY_DATES_TOGGLE=true`_

And that should be all you need to run your end-to-end tests — feature toggles on or off.

### Automating The Feature Toggles

As I said before, my team hosted our applications on Pivotal Cloud Foundry, and in PCF, we use things called VCAP services to hold our environment variables. Through manifest files we could bind these services to our applications and pass in environment variables — variables like if feature toggles should be turned on or off. See where I’m going here?

The check for the feature toggle environment variable makes deploying the app in an automated fashion easier. By including a simple environment variable (or not) to the manifest that deploys with each different stage of development, it’s easy to tell the application to check for settings in the config server (and employ them if need be) or not.

[![QA manifest that includes feature toggles](https://www.paigeniedringhaus.com/static/1353543796a724400b25393482e96286/3ddad/qa-manifest.png "QA manifest that includes feature toggles")](/static/1353543796a724400b25393482e96286/3ddad/qa-manifest.png)

_QA manifest screenshot._

QA manifest: note the `SPRING_PROFILES_ACTIVE` pointing to `QA`, `FEATURE_TOGGLE` environment variables with `‘on’`.

[![Production manifest that does not include feature toggles](https://www.paigeniedringhaus.com/static/e043e464058470192b22f20b6cbdea92/e9131/prod-manifest.png "Production manifest that does not include feature toggles")](/static/e043e464058470192b22f20b6cbdea92/e9131/prod-manifest.png)

_Production manifest screenshot._

Production manifest: no feature toggle in sight, and the `SPRING_PROFILES_ACTIVE` is set to `production`.

---

## Conclusion

There you have it. Now, you have seen how you can leverage a Spring Cloud Config server you’ve built with your Node.js application. Not only that, you can also configure end to end tests to run with the feature toggle on or off as well.

If you’ve found other ways to implement feature toggles or other nifty things like this in your own JavaScript projects, I’d love to hear about them.

Check back in a few weeks — I’ll be writing more about JavaScript, React, IoT, or something else related to web development.

If you’d like to make sure you never miss an article I write, sign up for my newsletter here: https://paigeniedringhaus.substack.com

Thanks for reading!

---

## Further References & Resources

- [Wikipedia, Feature Toggle](https://en.wikipedia.org/wiki/Feature_toggle)
- [NodeCloud Config Client](https://www.npmjs.com/package/nodecloud-config-client)
- [Sample Cloud Config Properties Repo](https://github.com/paigen11/spring-cloud-config-properties-sample)
- [Protractor documentation around environment params](https://moduscreate.com/blog/protractor_parameters_adding_flexibility_automation_tests/)