use connect middleware within other middleware #735

Closed
matthewmueller opened this Issue Jan 22, 2013 · 4 comments

2 participants

@matthewmueller

Sometimes while developing middleware, you want to depend on other middleware (bodyParser, basicAuth for example).

Looking for an easier way to develop modular middleware. Right now it's pretty ugly to work with:

function middleware(req, res, next) {
  // check if bodyparser/basicauth middleware already exist...
  connect.bodyParser()(req, res, function() {

     connect.basicAuth('matt', 'test')(req, res, function() {

     });
  });
}

A cleaner way would be to use a stack, like how connect does it. But that requires reimplementing what connect already does.

I'd be nice to either break out the stack logic as a separate module or expose a way of creating new stacks within middleware.

Then we could do something like this:

function handler(req, res, next) {
   stack(req, res, next)
     .use(connect.bodyParser())
     .use(connect.basicAuth('matt', 'test'))
     .use(middleware)
}
@tj
Sencha Labs member
tj commented Jan 22, 2013

that could be said about anything in node really, node code is just flat-out not nice to begin with haha, but I dont think we should be trying to patch that in Connect IMO

@matthewmueller

well i guess i'm just saying it'd be nice to have connect's stack logic in a separate repo (much like how send is in a separate repo).

there's quite a bit there (routing, error handling, etc), so a simple stacking function won't really do the trick.

@tj
Sencha Labs member
tj commented Jan 22, 2013

composing functions works fine, check out bodyParser, that's what it does

@matthewmueller

ahh okay fair enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment