Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

middleware-chain

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

middleware-chain

Chain your node.js middleware / functions

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Middleware Chain

Chain your node.js functions.

Middleware Chain allows you to simply chain your syncronous / asyncronous middleware functions. It works in a very similar way to how express handles middleware.

Works really well using the consign module to autoload your scripts.

Usage

chain([context], chain);
var chain = require('middleware-chain');

chain([ one, two, three ]);

// Optionally pass in context
var app = { hello: 'world' };
chain(app, [ one, two, three ]);

Please check out the examples or test folder for more!

Installation

$ npm install middleware-chain

Features

  • Chain functions / middleware
  • Works with both asyncronous and syncronous functions
  • Brings structure to scripts with a lot of callbacks
  • Allows modifiable flow
  • Ability to pass in initial context
  • Chains are nestable
  • Ability for parrallel chains
  • Test driven
  • Fast, Light-weight with no external dependencies

Context parameter

The optional context object is passed in as the first parameter, it can be an object containing anything you'd like to access throughout your middleware chain.

Chain parameter

The chain parameter is required, it's an array containing functions to be called in order.

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

Full Example

var chain = require('middleware-chain');

function one(context, next) {
  setTimeout(function() {
    context.one = 'Hello';
    console.log('Hello from one', context);
    return next();
  }, 1000);
}

function two(context, next) {
  setTimeout(function() {
    context.two = 'Hello';
    console.log('Hello from two', context);
    return next();
  }, 1000);
}

function three(context, next) {
  setTimeout(function() {
    context.three = 'Hello';
    console.log('Hello from three', context);
  }, 1000);
}

chain([ one, two, three ]);

Output

Hello from one
Hello from two
Hello from three

License

MIT

Keywords

FAQs

Package last updated on 23 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc