Skip to content

Instantly share code, notes, and snippets.

@levlaz
Created March 7, 2019 02:41
Show Gist options
  • Save levlaz/ab55334f42271572fd846f38f9b80807 to your computer and use it in GitHub Desktop.
Save levlaz/ab55334f42271572fd846f38f9b80807 to your computer and use it in GitHub Desktop.
const express = require('express')
const next = require('next')
const Featureflow = require('featureflow-node-sdk');
const LaunchDarkly = require('ldclient-node');
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const ldclient = LaunchDarkly.init(process.env.LD_SDK_KEY)
const featureflow = new Featureflow.Client({ apiKey: 'srv-env-685e066dea464f88be14effbf65cf69c' });
app.prepare()
.then(() => {
const server = express()
server.use((req, res, next) => {
let user = {
'key': 'user1',
'country': 'AU',
'custom': {
'page': 'index',
'roles': ['USER_ADMIN', 'BETA_CUSTOMER']
}
}
// let user = new Featureflow.UserBuilder("user1")
// .withAttribute('country', 'US')
// .withAttribute('page', 'index')
// .withAttributes('roles', ['USER_ADMIN', 'BETA_CUSTOMER'])
// .build();
// Using allFlagsState https://docs.launchdarkly.com/docs/node-sdk-reference#section-all-flags
// ldclient.on('ready', function () {
// ldclient.allFlagsState(user).then((allFlags) => {
// console.log(allFlags);
// req.features = allFlags;
// }
// )
// });
allFlags = ldclient.allFlagsState(user).then((allFlags) => {
req.features = allFlags;
});
return next();
});
server.get('/api/features', (req, res) => {
let user = {
'key': 'user2',
'country': 'AU',
'custom': {
'page': 'post',
'roles': ['USER_ADMIN', 'BETA_CUSTOMER']
}
}
allFlags = ldclient.allFlagsState(user).then((allFlags) => {
res.json(allFlags.toJSON());
});
// ldclient.on("ready", () => {
// ldclient.allFlagsState(user).then((allFlags) => {
// res.json(allFlags);
// }).catch(err => {
// console.log(err);
// res.status(500).send(err);
// })
// })
// let user = new Featureflow.UserBuilder("user2")
// .withAttribute('country', 'US')
// .withAttribute('page', 'post')
// .withAttributes('roles', ['USER_ADMIN', 'BETA_CUSTOMER'])
// .build();
//res.json(featureflow.evaluateAll(user));
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment