express-http-proxy
Version:
http proxy middleware for express
70 lines (48 loc) • 1.77 kB
Markdown
# express-http-proxy [](http://badge.fury.io/js/express-http-proxy) [](https://travis-ci.org/villadora/express-http-proxy) [](https://gemnasium.com/villadora/express-http-proxy)
Express proxy middleware to forward request to another host and pass response back
## Install
```bash
$ npm install express-http-proxy --save
```
## Usage
```js
var proxy = require('express-http-proxy');
var app = require('express')();
app.use('/proxy', proxy('www.google.com', {
forwardPath: function(req, res) {
return require('url').parse(req.url).path;
}
}));
```
If you only want to proxy get request
```js
app.use('/proxy', proxy('www.google.com', {
filter: function(req, res) {
return req.method == 'GET';
},
forwardPath: function(req, res) {
return require('url').parse(req.url).path;
}
}));
```
You can also intercept the response get by proxy before send it back to the client, or change the request options before it get sent to target:
```js
app.use('/proxy', proxy('www.google.com', {
forwardPath: function(req, res) {
return require('url').parse(req.url).path;
},
intercept: function(data, req, res, callback) {
data = JSON.parse(data.toString('utf8'));
callback(null, JSON.stringify(data));
},
decorateRequest: function(req) {
req.headers['Content-Type'] = '';
req.method = 'GET';
req.bodyContent = wrap(req.bodyContent);
return req;
}
}));
```
## Licence
MIT
<!-- do not want to make nodeinit to complicated, you can edit this whenever you want. -->