Is there a lightweight way to compose middlewares? #502

Closed
nponeccop opened this Issue Mar 6, 2012 · 4 comments

Comments

Projects
None yet
4 participants
@nponeccop

Sequenz library ( https://github.com/snd/sequenz/blob/master/lib/sequenz.coffee ) 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

This comment has been minimized.

Show comment Hide comment
@tj

tj Mar 6, 2012

Member

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);
Member

tj commented Mar 6, 2012

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);
@nponeccop

This comment has been minimized.

Show comment Hide comment
@nponeccop

nponeccop Mar 6, 2012

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

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

@tj tj closed this Mar 6, 2012

@tj tj reopened this Mar 6, 2012

@tj

This comment has been minimized.

Show comment Hide comment
@tj

tj Mar 6, 2012

Member

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

Member

tj commented Mar 6, 2012

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

@matomesc

This comment has been minimized.

Show comment Hide comment
@matomesc

matomesc Mar 20, 2012

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

// assume the following dir structure
/app
/app/routes
/app/routes/index.js
/app/routes/users.js

// 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 () {}
  }
}

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

// assume the following dir structure
/app
/app/routes
/app/routes/index.js
/app/routes/users.js

// 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