mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
Add followRedirects option
This commit is contained in:
parent
6f88caf6e4
commit
c9a556cfa5
@ -375,6 +375,7 @@ proxyServer.listen(8015);
|
|||||||
* **headers**: object with extra headers to be added to target requests.
|
* **headers**: object with extra headers to be added to target requests.
|
||||||
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
|
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
|
||||||
* **timeout**: timeout (in millis) for incoming requests
|
* **timeout**: timeout (in millis) for incoming requests
|
||||||
|
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
|
||||||
* **selfHandleRequest** true/false, if set to true, none of the webOutgoing passes are called and its your responsibility ro appropriately return the response by listening and acting on the `proxyRes` event
|
* **selfHandleRequest** true/false, if set to true, none of the webOutgoing passes are called and its your responsibility ro appropriately return the response by listening and acting on the `proxyRes` event
|
||||||
* **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:
|
* **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,15 @@
|
|||||||
var http = require('http'),
|
var httpNative = require('http'),
|
||||||
https = require('https'),
|
httpsNative = require('https'),
|
||||||
web_o = require('./web-outgoing'),
|
web_o = require('./web-outgoing'),
|
||||||
common = require('../common');
|
common = require('../common'),
|
||||||
|
followRedirects = require('follow-redirects');
|
||||||
|
|
||||||
web_o = Object.keys(web_o).map(function(pass) {
|
web_o = Object.keys(web_o).map(function(pass) {
|
||||||
return web_o[pass];
|
return web_o[pass];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var nativeAgents = { http: httpNative, https: httpsNative };
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Array of passes.
|
* Array of passes.
|
||||||
*
|
*
|
||||||
@ -99,6 +102,10 @@ module.exports = {
|
|||||||
// And we begin!
|
// And we begin!
|
||||||
server.emit('start', req, res, options.target || options.forward);
|
server.emit('start', req, res, options.target || options.forward);
|
||||||
|
|
||||||
|
var agents = options.followRedirects ? followRedirects : nativeAgents;
|
||||||
|
var http = agents.http;
|
||||||
|
var https = agents.https;
|
||||||
|
|
||||||
if(options.forward) {
|
if(options.forward) {
|
||||||
// If forward enable, so just pipe the request
|
// If forward enable, so just pipe the request
|
||||||
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
|
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
|
||||||
|
|||||||
@ -13,7 +13,8 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"eventemitter3": "^3.0.0",
|
"eventemitter3": "^3.0.0",
|
||||||
"requires-port": "^1.0.0"
|
"requires-port": "^1.0.0",
|
||||||
|
"follow-redirects": "^1.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"async": "^2.0.0",
|
"async": "^2.0.0",
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
|
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
|
||||||
httpProxy = require('../lib/http-proxy'),
|
httpProxy = require('../lib/http-proxy'),
|
||||||
expect = require('expect.js'),
|
expect = require('expect.js'),
|
||||||
|
url = require('url'),
|
||||||
http = require('http');
|
http = require('http');
|
||||||
|
|
||||||
describe('lib/http-proxy/passes/web.js', function() {
|
describe('lib/http-proxy/passes/web.js', function() {
|
||||||
@ -413,3 +414,39 @@ describe('#createProxyServer.web() using own http server', function () {
|
|||||||
http.request('http://127.0.0.1:8080/test2', function() {}).end();
|
http.request('http://127.0.0.1:8080/test2', function() {}).end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#followRedirects', function () {
|
||||||
|
it('should proxy the request follow redirects', function (done) {
|
||||||
|
var proxy = httpProxy.createProxyServer({
|
||||||
|
target: 'http://127.0.0.1:8080',
|
||||||
|
followRedirects: true
|
||||||
|
});
|
||||||
|
|
||||||
|
function requestHandler(req, res) {
|
||||||
|
proxy.web(req, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
var proxyServer = http.createServer(requestHandler);
|
||||||
|
|
||||||
|
var source = http.createServer(function(req, res) {
|
||||||
|
|
||||||
|
if (url.parse(req.url).pathname === '/redirect') {
|
||||||
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||||
|
res.end('ok');
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(301, { 'Location': '/redirect' });
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
proxyServer.listen('8081');
|
||||||
|
source.listen('8080');
|
||||||
|
|
||||||
|
http.request('http://127.0.0.1:8081', function(res) {
|
||||||
|
source.close();
|
||||||
|
proxyServer.close();
|
||||||
|
expect(res.statusCode).to.eql(200);
|
||||||
|
done();
|
||||||
|
}).end();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user