npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@optimizely/express

v0.2.1

Published

Express middleware for Optimizely

Downloads

1,270

Readme

Optimizely Express Middleware

This repository houses the Express.js integration for use with Optimizely Full Stack and Optimizely Rollouts.

Optimizely Full Stack is A/B testing and feature flag management for product development teams. Experiment in any application. Make every feature on your roadmap an opportunity to learn. Learn more at https://www.optimizely.com/platform/full-stack/, or see the documentation.

Optimizely Rollouts is free feature flags for development teams. Easily roll out and roll back features in any application without code deploys. Mitigate risk for every feature on your roadmap. Learn more at https://www.optimizely.com/rollouts/, or see the documentation.

Installation

Using NPM:

npm install @optimizely/express --save

Using yarn:

yarn add @optimizely/express

Configuration

const optimizelyExpress = require('@optimizely/express');
const optimizely = optimizelyExpress.initialize({
  sdkKey: 'CZsVVgn6j9ce6fNPt2ZEiB',
  datafileOptions: {
    autoUpdate: true,
    updateInterval: 600000 // 10 minutes in milliseconds
  },
});

The autoUpdate parameter indicates that your feature flags will get automatically updated from changes made in Optimizely's user interface. The updateInterval indicates how frequently the Optimizely Express SDK will poll for changes.

Usage

optimizely.middleware

app.use(optimizely.middleware);

The above middleware adds the following object on the express request object

req.optimizely:

req.optimizely = {
  datafile: A representation of all of your feature flags and experiments as defined in Optimizely
  client: The Optimizely SDK client instance which has methods for `isFeatureEnabled`, `activate`, `track`, etc.
}

Once the middleware is installed, you can use the standard Optimizely JavaScript SDK APIs like isFeatureEnabled

app.get('/', function(req, res, next) {
  const isEnabled = req.optimizely.client.isFeatureEnabled('checkout_flow', '123');
  res.render('index', {
    title: 'Express: ' + (isEnabled ? 'feature on!' : 'feature off')
  });
});

optimizely.webhookRequest

If you would rather that your server not poll for changes made in Optimizely's configuration but rather get updated as quickly as possible when changes occur, you can use the following secure webhook implementation. Note, this requires following the documentation on setting up a secure webhook in the Optimizely UI.

app.use('/webhooks/optimizely', bodyParser.text({ type: '*/*' }), optimizely.webhookRequest);

Once you have successfully implemented the webhook, you can turn off the polling completely by setting the autoUpdate attribute to false above in the configuration step.

Note: If you deploy your server to multiple different machines, this will not ensure that the two machines are in-sync with the latest configuration. If you would like to see support for cross-machine syncing via webhooks, please let us know by opening an issue on this repository.

optimizely.datafileRoute

If you would like to see the latest configuration of your feature flags and experiments as defined in Optimizely's UI, you can see the datafile by installing this route on your server:

app.use('/optimizely/datafile', optimizely.datafileRoute);

Demo

See the full demo of usage, reproduced below:

const optimizelyExpressSdk = require('@optimizely/express');
const optimizely = optimizelyExpressSdk.initialize({
  sdkKey: 'CZsVVgn6j9ce6fNPt2ZEiB',
  datafileOptions: {
    autoUpdate: true,
    updateInterval: 600000 // 10 minutes in milliseconds
  },
});

app.use(optimizely.middleware);

app.use('/webhooks/optimizely', bodyParser.text({ type: '*/*' }), optimizely.webhookRequest);

app.use('/optimizely/datafile', optimizely.datafileRoute);

app.get('/', function(req, res, next) {
  const isEnabled = req.optimizely.client.isFeatureEnabled('checkout_flow', '123');
  res.render('index', {
    title: 'Express: ' + (isEnabled ? 'feature on!' : 'feature off')
  });
});

About

@optimizely/express is developed and maintained by Optimizely and many contributors. If you're interested in learning more about what Optimizely X Full Stack can do for your company, please get in touch!

Contributing

Please see CONTRIBUTING.

Credits

This repository includes code from the following open source projects:

body-parser Copyright (c) 2014 Jonathan Ong [email protected] Copyright (c) 2014-2015 Douglas Christopher Wilson [email protected] License (MIT): https://github.com/expressjs/body-parser

cookie-parser Copyright (c) 2014 TJ Holowaychuk [email protected] Copyright (c) 2015 Douglas Christopher Wilson [email protected] License (MIT): https://github.com/expressjs/cookie-parser

debug Copyright (c) 2014 TJ Holowaychuk [email protected] License (MIT): https://github.com/visionmedia/debug

express Copyright (c) 2009-2014 TJ Holowaychuk [email protected] Copyright (c) 2013-2014 Roman Shtylman [email protected] Copyright (c) 2014-2015 Douglas Christopher Wilson [email protected] License (MIT): https://github.com/expressjs/express

http-errors Copyright (c) 2014 Jonathan Ong [email protected] Copyright (c) 2016 Douglas Christopher Wilson [email protected] License (MIT): https://github.com/jshttp/http-errors

jade Copyright (c) 2009-2014 TJ Holowaychuk [email protected] License (MIT): https://github.com/dscape/jade

morgan Copyright (c) 2014 Jonathan Ong [email protected] Copyright (c) 2014-2017 Douglas Christopher Wilson [email protected] License (MIT): https://github.com/expressjs/morgan

Additional Code

This software may be used with additional code that is separately downloaded by you. These components are subject to their own license terms, which you should review carefully.

request Copyright (c) 2010-2012 Mikeal Rogers License (Apache 2.0): https://github.com/request/request

request-promise Copyright (c) 2017, Nicolai Kamenzky, Ty Abonil, and contributors License (ISC): https://github.com/request/request-promise