This repository has been archived on 2020-11-02. You can view files and clone it, but cannot push or open issues or pull requests.
TripSit_Suite/node_modules/middleware-handler
2020-11-01 22:46:04 +00:00
..
.npmignore Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
.travis.yml Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
index.js Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
LICENSE Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
package.json Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
README.md Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00

MiddlewareHandler

Build Status

MiddlewareHandler manages custom middlewares in the same way as how Express/Connect does.

var MiddlewareHandler = require('middleware-handler');

var handler = new MiddlewareHandler();
handler.use(function(a, b, next) {
  console.log(a, b); // foo bar
  next();
});
handler.handle(['foo', 'bar']);

Installation

$ npm install middleware-handler

Example

Integration with Socket.io

Parsing cookie from handshakes data with the authorization.

var cookieParser = require('express').cookieParser('secret');
var handler = new MiddlewareHandler();
handler.use(function(handshakeData, next) {
  // handshakeData has almost the same attributes with request object.
  cookieParser(handshakeData, {}, next);
});

var io = require('socket.io').listen(8080);
io.set('authorization', function(handshakeData, callback) {
  handler.handle([handshakeData], function(err) {
    console.log(handshakeData.cookies);
    callback(err, !err);
  });
});

Documentation

#use(middleware)

middleware accepts variable arguments and a callback.

var handler = new MiddlewareHandler();
handler.use(function(arg, next) {
  var err;
  // do some stuff
  next(err);  // optionally accepts an error object
});
handler.use(function(arg, next) {
  next(null, 'foo'); // arguments will be passed to a next middleware
});
handler.use(function(arg, next) {
  console.log(arg);  // 'foo'
  next();
});

#handle([args], [callback])

Invokes middlewares.

var handler = new MiddlewareHandler();
handler.use(function(a, b, next) {
  console.log(a, b);  // 'foo bar'
  next();
});
handler.handle(['foo', 'bar'], function(err) {
  // after calling all middlewares
});

#compose([callback])

Creates a function which invokes middlewares.

var handler = new MiddlewareHandler();
handler.use(function(a, b, next) {
  console.log(a, b);  // 'foo bar'
  next();
});

var fn = handler.compose(function(err) {
    // after calling all middlewares
  });
fn('foo', 'bar');

#clear()

Clear all middlewares from the stack.

var handler = new MiddlewareHandler();
handler.use(function() {});
console.log(handler.stack.length);  // 1

handler.clear();
console.log(handler.stack.length);  // 0

compose(middlewares…)

Creates a function which invokes the passed middlewares.

function middleware(a, b, next) {
  console.log(a, b);  // 'foo bar'
  next();
}

var fn = MiddlewareHandler.compose(middleware, function(a, b) {
    console.log(a, b);  // 'foo bar'
  });
fn('foo', 'bar');

License

MIT