Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,24 @@ app.use('/', proxy('httpbin.org', {
}));
```

#### requestModules

There are times when you may want the proxy to use a custom request module to make the requests on behalf of the proxy. A good example of this is to use the `follow-redirects` library to have the proxy automatically follow redirects.

```js
var { http, https } = require('follow-redirects');

app.use('/', proxy('httpbin.org', {
requestModules: { http, https },
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
return {
...proxyReqOpts,
maxRedirects: 10,
};
}
}));
```

## Trace debugging

The node-debug module is used to provide a trace debugging capability.
Expand Down Expand Up @@ -575,6 +593,7 @@ app.use('/', proxy('internalhost.example.com', {

| Release | Notes |
| --- | --- |
| 1.7.0 | Support alternate request modules, such as from `follow-redirects`.
| 1.6.3 | [#453] Author should be able to delete headers in userResHeaderDecorator.
| 1.6.2 | Update node.js versions used by ci. |
| 1.6.1 | Minor bug fixes and documentation. |
Expand Down
4 changes: 1 addition & 3 deletions lib/requestOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';
var http = require('http');
var https = require('https');
var url = require('url');
var getRawBody = require('raw-body');
var isUnset = require('./isUnset');
Expand Down Expand Up @@ -45,7 +43,7 @@ function parseHost(Container) {
return {
host: parsed.hostname,
port: parsed.port || (ishttps ? 443 : 80),
module: ishttps ? https : http,
module: ishttps ? options.requestModules.https : options.requestModules.http,
};
}

Expand Down
30 changes: 24 additions & 6 deletions lib/resolveOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var debug = require('debug')('express-http-proxy');
var http = require('http');
var https = require('https');

var isUnset = require('../lib/isUnset');

Expand All @@ -15,10 +17,7 @@ function resolveBodyEncoding(reqBodyEncoding) {

// parse client arguments

function resolveOptions(options) {
options = options || {};
var resolved;

function judgeOptions(options) {
if (options.decorateRequest) {
throw new Error(
'decorateRequest is REMOVED; use proxyReqOptDecorator and' +
Expand All @@ -40,7 +39,20 @@ function resolveOptions(options) {
);
}

resolved = {
if (options.requestModules &&
!(options.requestModules.http || options.requestModules.https)
) {
throw new Error(
'requestModules requires at least one web protocol to be defined'
);
}
}

function resolveOptions(options) {
options = options || {};
judgeOptions(options);

var resolved = {
limit: options.limit || '1mb',
proxyReqPathResolver: options.proxyReqPathResolver
|| options.forwardPathAsync
Expand All @@ -63,7 +75,13 @@ function resolveOptions(options) {
https: options.https,
port: options.port,
reqAsBuffer: options.reqAsBuffer,
timeout: options.timeout
timeout: options.timeout,

/*
* Allow optional injection of http and https request modules,
* such as from `follow-redirects`.
*/
requestModules: options.requestModules || { http: http, https: https }
};

// automatically opt into stream mode if no response modifiers are specified
Expand Down
Loading