Hey all! I am new to GrowthBook, and I have a cou...
# ask-questions
f
Hey all! I am new to GrowthBook, and I have a couple of questions. I had successfully installed and used GrowthBook feature toggles in our frontend app (ReactJS), but I am having trouble doing the same in our backend app (KoaJS). I tried following the JavaScript SDK but without any luck. Does anyone have any insights? More specifically: • Where should I install the Growthbook package? In our main
app.js
? And if so, is there any guide that I can follow? • How can I use GrowthBook features inside a service? Consider that some services we use are used directly from other services and are not attached to any route. I want to access the GrowthBook instance on those services. I tried exhausting all possible solutions before deferring to you folks. Any help would be much appreciated. Thanks in advance!
f
Hi John, I’m not familiar with KoaJS, but it should work. What is the failure mode for the backend not working? an error or the flags are just not working correctly?
f
👋 Hi Graham, and thank you for the swift response. The failure mode is that I am not getting the features I set up inside the GrowthBook dashboard. Here is a trimmed example of this service:
Copy code
const db = require('db');
const { GrowthBook } = require('@growthbook/growthbook');
const { GROWTHBOOK_SETTINGS } = require('lib/constants/growthbook.constants');

const {
  DATABASE_DOCUMENTS
} = require('app.constants');

const service = db.createService(DATABASE_DOCUMENTS.TESTS);

// Initialize GrowthBook
const growthbook = new GrowthBook({
  apiHost: GROWTHBOOK_SETTINGS.API_URL,
  clientKey: GROWTHBOOK_SETTINGS.CLIENT_KEY,
  enableDevMode: GROWTHBOOK_SETTINGS.ENABLE_DEV_MODE,
});

growthbook.loadFeatures();

service.completeTest = async (test) => {

  if (growthbook.isOn(GROWTHBOOK_FEATURE_FLAGS.ENABLE_AI_TEST_SUMMARY)) {
   console.log('It should work');
   // I want to trigger a specific event here
  }

  // Some additional business logic...
};

module.exports = service;
When I console into my client app (ReactJS), I see both features, but when I do the same in the backend, I am not seeing anything. One of my questions is if this is the right place to initialize GrowthBook, or if it should happen on an app level.
f
huh, I wonder if its a race condition - maybe add an await on the load features call?
We have some examples using Node server side: https://docs.growthbook.io/lib/js#nodejs
Until features are loaded, all features will evaluate to
null
. If you’re ok with a potential flicker in your application (features going from
null
to their real value), you can call
loadFeatures
without awaiting the result.
If you want to refresh the features at any time (e.g. when a navigation event occurs), you can call
gb.refreshFeatures()
.
f
Thanks for the suggestions but I don’t have any luck 😞 I moved the
loadFeatures
inside my async function but I am still seeing nothing.
Copy code
try {
    growthbook.loadFeatures({
      // When features change, update the GrowthBook instance automatically
      // Default: `false`
      autoRefresh: true,
      // If the network request takes longer than this (in milliseconds), continue
      // Default: `0` (no timeout)
      timeout: 2000,
    });
  } catch (error) {
    console.error('Error loading GrowthBook features:', error);
  }
Until features are loaded, all features will evaluate to
null
.
Is there any function to check if features are loaded?
Also, is there any channel here to ask for specific help from experts? Paid of course!
h
I haven't used Koa before, but my understanding is that it's fairly similar to Express. First, check out the NodeJS guide (which is really geared towards Express). Specifically there's a section starting with:
Create a separate GrowthBook instance for every incoming request. This is easiest if you use a middleware:
```// Example using Express
app.use(function(req, res, next) {
// Create a GrowthBook instance and store in the request
req.growthbook = new GrowthBook({
etc...```
You'd create a middleware of this sort (only Koa-flavored) near the top of your routes, and then subsequent routes would have access. Just be sure to not call
next()
until
loadFeatures()
has completed; otherwise your subsequent routes will not have the data available. The example above is Express, but Koa seems to do something quite similar, except that your middleware interface would be
function(ctx, next)
(no req, res). So I believe instead of assigning the GrowthBook SDK to the
req.growthbook
object (Express), you'd assign it to Koa's
ctx.state.growthbook
. Then your downstream routers / controllers would have access to that context variable.
147 Views