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

feature-toggle-express-middleware

v0.1.3

Published

feature-toggle-express-middleware ================================= This middleware manages feature toggles. You can switch on/off features using a query parameter. It stores them in a cookie. ```js const featureToggle = require('feature-toggle-express-mi

Downloads

9

Readme

feature-toggle-express-middleware

This middleware manages feature toggles. You can switch on/off features using a query parameter. It stores them in a cookie.

const featureToggle = require('feature-toggle-express-middleware');

// pass a list of allowed features and a prefix representing your application
const featureToggleMiddleware = featureToggle(['feature1', 'feature2'], 'myapp');

app.get('/myapp', featureToggleMiddleware, (req, res) => {
  if (res.locals.hasToggle('feature1')) {
    res.send('Feature 1 active');
  }
  res.send('Feature 1 not active');
});

featureToggle takes this arguments:

  • an array with allowed toggles (a toggle is a string)
  • a prefix that is used for the cookie and the query parameters
  • [optional] the options for the cookie (see expressjs documentation)
  • [optional] a boolean that when true, disable automatic cache control headers

Using the middleware

You can switch a feature on using the addToggles query parameter:

http://www.example.com?myappAddToggles=feature1

You can also enable multiple features:

http://www.example.com?myappAddToggles=feature1,feature2

You can remove one or more feature:

http://www.example.com?myappRemoveToggles=feature1
http://www.example.com?myappRemoveToggles=feature1,feature2

You can use both query parameter to add/remove toggles

http://www.example.com?myappRemoveToggles=feature1&myappAddToggles=feature2

You can switch off all toggles:

http://www.example.com?myappResetToggles=true

allow-feature

This is another middleware that, used together with featureToggle enable/disable a specific endpoint.

const allowFeature = require('feature-toggle-express-middleware/allow-feature');

const disableWithFeature1 = allowFeature.allowWith('feature1');
const enableWithFeature1 = allowFeature.disallowWith('feature1');

app.get('/myapp/1', featureToggleMiddleware, disableWithFeature1, (req, res) => {
  console.log('this is disabled with feature1');
});

app.get('/myapp/2', featureToggleMiddleware, enableWithFeature1, (req, res) => {
  console.log('this is enabled with feature1');
});

Notes

This module is implemented with ES6 feature. Use it with the appropriate node versions. The middleware provides to disable cache (using cache headers), when a feature toggle is enabled.