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

connect-chain-if

v2.0.0

Published

Connect middleware that composes a new middleware from a chain of middlewares. Supports conditional chaining.

Downloads

34

Readme

Connect-Chain-If

"connect-chain-if" is a middleware that composes a new middleware of type function(req, res, next) from a chain of middlewares to be called in series. The new middleware is fully connect compliant.

An error passed to next or thrown in a middleware will short circuit the chain and be passed to the next error middleware of type function(err, req, res, next) in the connect middleware chain.

You can use "connect-chain-if" to build up new middlewares from existing connect-style-middlewares using the conditional expressions chain.if() and chain.switch().

To allow breaking up long lasting middleware chains, all middlewares within a chain are processed via the node event loop using process.nextTick instead of being processed synchroniously. Use chain.nextTick(false); to revert to the synchronous behaviour.

Usage

Building new middlewares

var
  express = require('express'),
  chain = require('connect-chain-if');

var
  app = express(),
  middleware1 = function(req, res, next) {
    res.body = req.url + '\n';
    next && next();
  },
  middleware2 = function(req, res, next) {
    res.writeHead(200);
    res.write(res.body || '');
    res.end();
    next && next();
  };

var newMiddleware = chain([ middleware1, middleware2 ]);
// or to maintain backward compatibility
newMiddleware = chain(middleware1, middleware2);

// then use it in express/ connect
app.use(newMiddleware);

app.listen(3000);

Building middlewares with error traps

var
  chain = require('connect-chain-if');

var
  middleware1 = function(req, res, next) {
    // ... something bad happened
    console.log('middleware1');
    next && next(new Error('error'));
  },
  middleware2 = function(req, res, next) {
    console.log('middleware2');
    next && next();
  },
  middleware3 = function(req, res, next) {
    console.log('middleware3');
    next && next();
  },
  middlewareErr = function(err, req, res, next) { // note the "4" arguments
    console.log('trapped error:', err.message);
    next && next();
  };
    next && next();
  };

chain([
  middleware1,
  middleware2,
  middlewareErr,
  middleware3
])({},{});

In this example middleware2 will be bypassed. Output is:

middleware1
trapped error: error
middleware3

Using connect middlewares without connect/ express

var
  http = require('http'),
  chain = require('connect-chain-if');

var
  middleware1 = function(req, res, next) {
    if (/^\/[a-z]?$/.test(req.url)) {
      res.body = req.url;
      next && next();
    }
    else {
      next && next(new Error('err'));
    }
  },
  middleware2 = function(req, res, next) {
    res.writeHead(200);
    res.write(res.body || '');
    res.end();
  },
  middlewareErr = function(err, req, res, next) {
    res.writeHead(404);
    res.end();
  };

http.createServer(chain(middleware1, middleware2, middlewareErr))
  .listen(3000, 'localhost');

NOTE: Take care that in this case your very last middleware in the chain handles any error.

curl -v localhost:3000/a
#> < HTTP/1.1 200 OK
#> /a
curl -v localhost:3000/aaa
#> < HTTP/1.1 404 Not Found

Creating conditional middlewares

You can either use chain.if or chain.switch to define conditional middlewares.

var
  chain = require('connect-chain-if');

var middleware = function(req, res, next) {
  chain([
    chain.if(
      /*if */ /^\/$/.test(req.url), [ // defines a new middleware chain here
        function(req, res, next) {
          res.body = 'homepage';
          next && next();
        },
        function(req, res, next) {
          res.statusCode = 200;
          next && next();
      }],
      /*if */ /^\/error500$/.test(req.url), function(req, res, next) {
        res.body = 'weired';
        res.statusCode = 500;
        next && next();
      },
      /*else */ function(req, res, next) {
        res.body = 'no idea';
        res.statusCode = 404;
        next && next();
      }
    ),
    function (req, res, next) {
      console.log(res.statusCode, res.body);
      next && next();
    }
  ])(req, res, function (err) {
    next && next(err);
  });
};

middleware({ url: '/' }, {});
//> 200 'homepage'
middleware({ url: '/error500'}, {});
//> 500 'weired'
middleware({ url: '/something'}, {});
//> 404 'no idea'

Using chain.switch:

var
  chain = require('connect-chain-if');

var middleware = function(req, res, next) {
  chain([
    chain.switch(
      /*switch */ req.url,
      /*case */ '/', [ // defines a new middleware chain here
        function(req, res, next) {
          res.body = 'homepage';
          next && next();
        },
        function(req, res, next) {
          res.statusCode = 200;
          next && next();
      }],
      /*case */ '/error500', function(req, res, next) {
        res.body = 'weired';
        res.statusCode = 500;
        next && next();
      },
      /*default */ function(req, res, next) {
        res.body = 'no idea';
        res.statusCode = 404;
        next && next();
      }
    ),
    function (req, res, next) {
      console.log(res.statusCode, res.body);
      next && next();
    }
  ])(req, res, function (err) {
    next && next(err);
  });
};

middleware({ url: '/' }, {});
//> 200 'homepage'
middleware({ url: '/error500'}, {});
//> 500 'weired'
middleware({ url: '/something'}, {});
//> 404 'no idea'

License

Software is released under MIT.