Git Product home page Git Product logo

middleware-chain's Introduction

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')
  , app = { main: 'Hello' }
;

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(app, [ one, two, three ]);

Output

Hello from one, { main: 'Hello', one: 'Hello' }
Hello from two, { main: 'Hello', one: 'Hello', two: 'Hello' }
Hello from three, { main: 'Hello', one: 'Hello', two: 'Hello', three: 'Hello' }

License

MIT

middleware-chain's People

Contributors

utkarsh85 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.