resilient-http / resilient.js

Fault tolerant and reactive HTTP client for node.js and browsers
183 stars 13 forks source link

Support bidirectional middlewares #118

Closed h2non closed 9 years ago

h2non commented 9 years ago

Useful when using multiple discovery servers as Consul:

var client = Resilient()
var consulMiddleware = require('resilient-consul')

client.use(consulMiddleware({
  service: 'api'
}))

// Middleware implementation
function (params) {
  return function (options, resilient) {
    // Interface
    return {
      type: 'discovery',
      in: function (options, next) {
        options.qs.service = 'test'
        next()
      },
      out: function (res, req, next) {
        var servers = res.body.map(function (item) {
          return item.nested.url
        })
        next(null, servers)
      }
    }
  }
}
h2non commented 9 years ago

Done!