I'm working on a app (using Connect not Express) composed of a set of middlewares plus node-http-proxy module, that is, I have a chain of middlewares like:
midA -> midB -> http-proxy -> midC
In this scenario, the response is wrote by the http-proxy that proxies the request to some target and returns the content.
I would like to create a middleware (say midB
) to act as a cache. The idea is:
- If url is cached the cache-middleware writes the response and avoids continuing the middleares chain.
- If url is not cached the cache-middleware passes the request within the middlewares chain bit requires to read the final response content to be cached.
How can achieve this? Or there is another approach?
Cheers
Answering myself. If you have a middleware like
function(req, res, next){..}
and need to read the content of the response object.In this case the
res
is ahttp.ServerResponse
object, a writable stream where every middleware in the chain is allowed to write content that will conform the response we want to return.The way I found to read the content all middlewares write to the response is redefining the
write
method:Any other solutions will be appreciated.