future-caravan-57043
04/02/2023, 5:26 PMapp.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!fresh-football-47124
future-caravan-57043
04/02/2023, 6:15 PMconst 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.fresh-football-47124
fresh-football-47124
fresh-football-47124
Until features are loaded, all features will evaluate to. If you’re ok with a potential flicker in your application (features going fromnull
to their real value), you can callnull
without awaiting the result.loadFeatures
If you want to refresh the features at any time (e.g. when a navigation event occurs), you can call.gb.refreshFeatures()
future-caravan-57043
04/02/2023, 6:26 PMloadFeatures
inside my async function but I am still seeing nothing.
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 toIs there any function to check if features are loaded?.null
future-caravan-57043
04/02/2023, 6:29 PMhappy-autumn-40938
04/03/2023, 4:18 AMCreate 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.Open source platform for stress free deployments, measured impact, and smarter decisions.
Powered by