'use strict'; const express = require('express'); const unleash = require('unleash-client'); const chalk = require('chalk'); const app = express(); const instance = unleash.initialize({ appName: 'demo-app', url: 'http://localhost:4242/', refreshIntervall: 4000, metricsInterval: 10000, strategies: [ new unleash.Strategy('extra', true), ], }); instance.on('ready', () => { console.log('connected to unleash'); setInterval(() => { const result = unleash.isEnabled('add-feature-2', null, Boolean(Math.round(Math.random() * 2))); console.log(chalk.yellow('add-feature-2'), chalk.blue(result.toString())); }, 1000); setInterval(() => { const result = unleash.isEnabled('toggle-2', null, Boolean(Math.round(Math.random() * 2))); console.log(chalk.green('toggle-2'), chalk.blue(result.toString())); }, 1500); setInterval(() => { const result = unleash.isEnabled('toggle-3', null, Boolean(Math.round(Math.random() * 2))); console.log(chalk.red('toggle-3'), chalk.blue(result.toString())); }, 1500); }); instance.on('error', (err) => { console.error(err.message, err.stack); }); instance.on('warn', console.warn); function outputFeature (name, feature) { if (feature.enabled === false) { return; } return `

${name}

`; } app.get('/', (req, res) => { const { data } = instance.repository.storage; res.send(` Demo example unleash-client usage ${ Object.keys(data) .map((key) => outputFeature(key, data[key])) .filter(Boolean) .join('
') } `); }); app.listen(process.env.PORT || 1337);