Code Monkey home page Code Monkey logo

midware's Introduction

midware Build Status

midware is a tiny module to create domain-agnostic middleware layers for any node.js or browser application. Inspired in the middleware pattern by connect.

It's just ~80 SLOC.

Example

var use = require('midware')()
var message = {}

use(function(msg, next) {
  // msg === message
  next()
})

use.run(message, function(err) {
  if (err) return console.log(err)
  // finished
})

Installation

Node.js

npm install midware

Browser

Via bower:

bower install midware

Via component:

component install h2non/midware

Loading the script:

<script src="//cdn.rawgit.com/h2non/midware/0.1.7/midware.js"></script>

Test

To run tests use npm.

$ npm install
$ npm test

Documentation

Basic Usage

Middleware is useful for creating a plugin system or configuring anything within an application. To use midware just require it and make a call to the module.

var midware = require('midware')
var use = midware()

This will return a use function which when passed a callback will add it a waterfall sequence that will be invoked one after the other whenever the middleware is run.

use(function(next) {
  // mad science here
  next()
})

Callbacks are given a next function which will always be the last argument. Calling next will tell the middleware to call the next callback in the use sequence or will complete its run. To run the callback sequence call the method run on the use function.

use.run(function(err) {
  if (err) { return console.log(err) }
  // all done professor
})

run takes any amount of parameters that the callbacks will passed whenever run.

use(function(first, last, next) {
  console.log('Hello %s, %s', first, last)
  next()
})
use.run('Christopher', 'Turner')

Stopping

Whenever a callback should throw an exception or wish to stop the middleware from running any more calls. Give next an error or explicitly tell it stop.

use(function(next) {
  next(new Error()) // stops middleware and gives error
  next(null, true) // tells middleware to stop
})

use.run(function (err, ended) {
  // ...
})

Apply Context

Instead of binding context to callbacks, send the context to midware.

var context = {}
var use = midware(context)
use(function(next) {
  // this === context
  next()
})
use.run(function(err) {
  // this === context
})

Removing a function

You can remove registered functions in the middleware via its function name or function reference

var use = midware()
use(function test(next) {
  next()
})
use.remove('test') // by function name
var use = midware()
function test(next) {
  next()
}
use(test)
use.remove(test) // by function reference

API

midware([context])

use(callback...)

use.remove(name|function)

use.run([args...], [done])

use.flush()

use.stack = [ function... ]

License

MIT

Copyright (c) 2014 Christopher Turner Copyright (c) 2015 Tomas Aparicio

midware's People

Contributors

h2non avatar tur-nr avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.