Treat a sequence of middleware as middleware.
Install the module with:
npm install composable-middleware
var composable_middleware = ;
Middleware is a function that follows specific conventions. Composable middleware allows a series of middleware functions to be used as if they were a single middleware function. With it, a middleware stack is middleware and becomes a first-class object. With composable middleware you can:
You may compose middleware by defining its steps in the argument to the composable-middleware function:
var mw =;
or by using its
var mw =;
and then include it in another middleware stack:
var okay =
You might then use that middleware in Connect, Express or any other middleware-based framework:
var app = ;app;app;
One of the design goals of this package is to minimize overhead when sequencing through middleware steps. Notably, it does not support mounting paths or routing. It does not examine the URL at all. That is best left to middleware, perhaps middleware that routes requests into different middleware stacks depending upon route.
The composable_middleware software also does not deal with errors or unhandled requests other than routing errors to middleware that expects an error argument. Since a middleware stack may be treated as middleware within another middleware stack, it cannot be assumed that reaching the end of the stack has any significance. You will need to provide middleware (perhaps we should call it 'finalware') to send out 404 responses or to log errors and send 500 responses if the overall framework in which the middleware is running does not do so.
The Composable Middleware package supports Connect middleware and flatiron/union middleware as well as a hybrid of the two.
(). The request and response are in
this.res.emit('next')passes the request to the next level of middleware.
(next). The request and response are in
(err,next). The request and response are in
The middleware type is determined by checking its arity:
Error-handling middleware is skipped unless an error was passed to the
next function. Normal middleware is skipped if there is an error.
Although not a convention shared by other middleware containers, this package calls all middleware serving a given request in the same context. It should thus be possible to attach an attribute to
this in one middleware function and access it in a different middleware function serving the same request. This package will assure, however, that the global object is not used as the context, allocating a new object to serve as the context, if needed. To avoid the possibility of interference, if invoked as Connect middleware,
composable-middleware will similarly replace the Connect context with a new context. If a new context is created,
this._middleware_common_object will be defined. The constructor for the new context is exported as MiddlewareCommonObject and the function used by
composable-middleware to test whether a new context is needed is exported as
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
thisobject for all middleware serving a given request.
Copyright (c) 2013 Randy McLaughlin Licensed under the MIT license.