Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
feat(#81): add docs for middleware API
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Dec 4, 2015
1 parent 0cc5496 commit a7b6452
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,21 @@ In other words, you can create or use middleware as you already know with the ty

As a kind of inversion of control, `rocky` exposes a tiny API in every `http.ClientRequest` passed via the middleware layer:

##### Request

- **req.rocky** `object`
- **.options** `object` - Expose the [configuration](#configuration) options for the current request.
- **.proxy** `Rocky` - Expose the rocky instance. Use only for hacking purposes!
- **.route** `Route` - Expose the current running route. Only available in `route` type middleware
- **req.stopReplay** `boolean` - Optional field internally checked by `rocky` to stop the request replay process.

##### Response

- **res.rocky** `object`
- **.options** `object` - Expose the [configuration](#configuration) options for the current request.
- **.proxy** `Rocky` - Expose the rocky instance. Use only for hacking purposes!
- **.route** `Route` - Expose the current running route. Only available in `route` type middleware

This allows you way to extend or modify specific values from the middleware layer with HTTP request flow accuracy level, and therefore without having side-effects with other routes.

Example replacing the target server URL:
Expand Down
36 changes: 18 additions & 18 deletions lib/protocols/http/passes/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,6 @@ function forwardRetryRequest (route, opts, forwardReq, res, done) {
}
}

function forwardRequest (route, opts, forwardReq, res, done) {
const proxy = new HttpProxy

// Use the proper request body
useRequestBody(forwardReq, opts)

// Prepare propagation of proxy events to parent bus
propagateEvents(proxy, route, finisher)

// Finally forward the request
proxy.web(forwardReq, res, opts, finisher)

function finisher () {
cleanup(proxy)
done.apply(null, arguments)
}
}

function defineHostHeader (forwardReq, opts) {
const forwardHost = opts.forwardHost
if (!forwardHost) return
Expand All @@ -127,6 +109,24 @@ function defineHostHeader (forwardReq, opts) {
if (host) headers.host = host
}

function forwardRequest (route, opts, forwardReq, res, done) {
const proxy = new HttpProxy

// Use the proper request body
useRequestBody(forwardReq, opts)

// Prepare propagation of proxy events to parent bus
propagateEvents(proxy, route, finisher)

// Finally forward the request
proxy.web(forwardReq, res, opts, finisher)

function finisher () {
cleanup(proxy)
done.apply(null, arguments)
}
}

function useRequestBody (forwardReq, opts) {
var body = forwardReq.body

Expand Down

0 comments on commit a7b6452

Please sign in to comment.