Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a lightweight way to compose middlewares? #502

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

Is there a lightweight way to compose middlewares? #502

nponeccop opened this issue Mar 6, 2012 · 4 comments

Comments

@nponeccop
Copy link

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
Copy link
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
Copy link
Author

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

@tj tj closed this as completed Mar 6, 2012
@tj tj reopened this Mar 6, 2012
@tj
Copy link
Member

tj commented Mar 6, 2012

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

@matomesc
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants