Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Is there a lightweight way to compose middlewares? #502

Closed
nponeccop opened this Issue · 4 comments

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

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
Owner
tj commented

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

@matomesc

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
Something went wrong with that request. Please try again.