connectr

Connect wrapper that adds ability to insert middleware at arbitrary positions."

npm install connectr
9 downloads in the last day
53 downloads in the last week
310 downloads in the last month

Connectr for Connect (Node.js)

NPM version

Connectr is a layer on top of Connect that allows the insertion/removal of middlewares after the stack has been built. This is especially useful when you don't have access to the code that sets up your Connect stack (a third party module for example).

Install

npm install connectr

Usage

var connectr = require('connectr')(app);

// you can also monkey patch app directly:
// app = require('connectr').patch(app)

// Add labeled middleware
connectr.use(middleware).as(label);

// Label middleware which is at a specific position on the stack
// This should be used only if you don't have access to the code
// that builds the stack.
//
// Tip: use `app.stack` to inspect the stack
connectr.index(index).as(label);

// Insert before middleware
connectr.before(label).use(middleware).as(label);

// Insert after middleware
connectr.after(label).use(middleware);

// Insert at beginning of stack
connectr.first().use(middleware);

// Remove middleware
connectr.remove(label);

// Assign a label to middleware already in the stack
connectr.index(i).as(label);

// the .as, .before and .after calls are optional

// have a problem? try console.log(app.stack)

Simple Example

var connect = require('connect'),
var app = connect();
var connectr = require('connectr')(app);

connectr.use(connect.cookieParser).as('cookieParser');

/* ... */

connectr.before('cookieParser').use(function (req, res, next) {
  console.log('Before cookie parser...');
  next();
}).as('log before cookie parser');

Kitchen Sink Example

var http = require('http'),
  connect = require('connect'),
  app = connect(),
  connectr = require('connectr')(app);

var cookieParser = connect.cookieParser();

// we need to manually label middlewares
cookieParser.label = 'cookieParser';

app.use(cookieParser);

connectr.before('cookieParser').use(function (req, res, next) {
  console.log('Middleware before cookie parser.');
  next();
});

connectr.after('cookieParser').use(function (req, res, next) {
  console.log('Middleware after cookie parser.');
  next();
});

// you can also use connectr to label your middlewares
// instead of labeling them manually as above

connectr.use(connect.bodyParser()).as('bodyParser');

connectr.use(function (req, res, next) {
  console.log('Last middleware');       
  res.end('Done!');
});

connectr.before('bodyParser').use(function (req, res, next) {
  console.log('Before body parser');
  next();
}).as('beforeBodyParser');

connectr.after('beforeBodyParser').use(function (req, res, next) {
  console.log('I should be called after beforeBodyParser but before bodyParser');
  next();
}).as('betweenBeforeBodyParserAndBodyParser');

connectr.after('bodyParser').use(function (req, res, next) {
  console.log('After body parser');
  next();
}).as('afterBodyParser');

//console.log(app.stack);

http.createServer(app).listen(3000);

License

MIT: http://olalonde.mit-license.org

npm loves you