Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Is there a lightweight way to compose middlewares? #502

nponeccop opened this Issue · 4 comments

4 participants


Sequenz library ( ) provides a helper:

function handler(req, res)

app.get('/frame', sequenz.sequence([connect.compress(), handler]))

What is the recommended way to compose middlewares this way? So whatever handler outputs gets compressed?

tj commented

not really anything specific no, you could just have your own called compress that takes N other middleware. For express you can just pass several and they get called in sequence but Connect doesn't have anything special

app.get('/frame', connect.compress(), handler);

So there's no general builtin way to compose middleware, but Express router has such feature built in?

@tj tj closed this
@tj tj reopened this
tj commented

Express just applies any number of middleware you want to a given route, validate things, blah blah


You can always make your own conventions. Sometimes i do the following which keeps everything clean:

// assume the following dir structure

// index.js actually reads the files in `/app/routes` and builds a route index,
// maps the `exports.routes`, and then finally re-exports everything so you can do
// require('./routes') and get all of your routes elsewhere

// then every file in ./routes looks something like:
// users.js
exports.routes = {
  // now this object basically maps to all of your resources using like so:
  '/users': {
    get: function () {},
    post: function () {}
  '/users/:id': {
    get: function () {},
    put: function () {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.