Skip to content

Connectr allows the insertion/removal of Express middlewares at "runtime". LOOKING FOR MAINTAINER

License

Notifications You must be signed in to change notification settings

shauntarves/connectr

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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).

This module also supports Express.

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

About

Connectr allows the insertion/removal of Express middlewares at "runtime". LOOKING FOR MAINTAINER

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%