Olivier Lalonde's blog

Hey. I'm a friendly hacker and startup guy. I tweet as @o_lalonde.

Connectr: Programmatically Manipulating the Connect Stack

Connectr is a Node.js module I wrote that aims to solve the following problem: how to manipulate the Connect stack after it has been created.

This is a problem I faced while writing a REST framework on top of Express.js. The framework initialized the middleware stack but there was no easy way to expose the stack to end users of the framework so they could add their own middlewares at arbitrary positions on the stack.

Install

npm install connectr

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var connectr = require('connectr')(app);

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

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

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

// Remove middleware
connectr.remove(label);

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

Simple Example

1
2
3
4
5
6
7
8
9
10
11
12
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');

I am hoping the methods that Connectr provide can eventually be supported natively by Connect but until then, Connectr does the job.

If you use Connectr, please star the project on Github to show your support.

Happy coding!

Want to get notified about more articles like this? Follow @o_lalonde on Twitter.

Comments