Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
node-http-proxy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
node-http-proxy
Commits
bde98f48
Commit
bde98f48
authored
Aug 02, 2010
by
Marak Squires
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changed api to better reflect nodes api. updated demos, tests, docs
parent
fb8c5abd
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
16 deletions
+28
-16
README.md
README.md
+18
-6
demo.js
demo.js
+3
-3
lib/node-http-proxy.js
lib/node-http-proxy.js
+2
-2
test/node-http-proxy-test.js
test/node-http-proxy-test.js
+5
-5
No files found.
README.md
View file @
bde98f48
...
@@ -30,12 +30,12 @@ Let's suppose you were running multiple http application servers, but you only w
...
@@ -30,12 +30,12 @@ Let's suppose you were running multiple http application servers, but you only w
npm install http-proxy
npm install http-proxy
</pre>
</pre>
### How to
use node-http-proxy
### How to
setup a basic proxy server
<pre>
<pre>
var http = require('http'),
var http = require('http'),
httpProxy = require('http-proxy');
httpProxy = require('http-proxy');
httpProxy.createServer('
localhost', '9000
').listen(8000);
httpProxy.createServer('
9000', 'localhost
').listen(8000);
http.createServer(function (req, res){
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.writeHead(200, {'Content-Type': 'text/plain'});
...
@@ -46,24 +46,36 @@ Let's suppose you were running multiple http application servers, but you only w
...
@@ -46,24 +46,36 @@ Let's suppose you were running multiple http application servers, but you only w
see the
[
demo
](
http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js
)
for further examples.
see the
[
demo
](
http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js
)
for further examples.
### How to
use node-http-proxy
with custom server logic
### How to
setup a proxy server
with custom server logic
<pre>
<pre>
var http = require('http'),
var http = require('http'),
httpProxy = require('http-proxy');
httpProxy = require('http-proxy');
// create a proxy server with custom application logic
// create a proxy server with custom application logic
httpProxy.createServer(function (req, res, proxy) {
httpProxy.createServer(function (req, res, proxy) {
// Put your custom server logic here
// Put your custom server logic here
proxy.proxyRequest('
localhost', '9000
', req, res);
proxy.proxyRequest('
9000', 'localhost
', req, res);
}).listen(8000);
}).listen(8000);
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied: ' + req.url +'
\n
' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
</pre>
### How to proxy requests with a regular http server
<pre>
var http = require('http'),
httpProxy = require('http-proxy');
// create a regular http server and proxy its handler
// create a regular http server and proxy its handler
http.createServer(function (req, res){
http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy;
var proxy = new httpProxy.HttpProxy;
proxy.watch(req, res);
proxy.watch(req, res);
// Put your custom server logic here
// Put your custom server logic here
proxy.proxyRequest(
'localhost', 9000
, req, res);
proxy.proxyRequest(
9000, 'localhost'
, req, res);
}).listen(8001);
}).listen(8001);
http.createServer(function (req, res){
http.createServer(function (req, res){
...
...
demo.js
View file @
bde98f48
...
@@ -41,13 +41,13 @@ sys.puts(welcome.rainbow.bold);
...
@@ -41,13 +41,13 @@ sys.puts(welcome.rainbow.bold);
/****** basic http proxy server ******/
/****** basic http proxy server ******/
httpProxy
.
createServer
(
'
localhost
'
,
9000
).
listen
(
8000
);
httpProxy
.
createServer
(
9000
,
'
localhost
'
).
listen
(
8000
);
sys
.
puts
(
'
http proxy server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8000
'
.
yellow
);
sys
.
puts
(
'
http proxy server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8000
'
.
yellow
);
/****** http proxy server with latency******/
/****** http proxy server with latency******/
httpProxy
.
createServer
(
function
(
req
,
res
,
proxy
){
httpProxy
.
createServer
(
function
(
req
,
res
,
proxy
){
setTimeout
(
function
(){
setTimeout
(
function
(){
proxy
.
proxyRequest
(
'
localhost
'
,
9000
,
req
,
res
);
proxy
.
proxyRequest
(
9000
,
'
localhost
'
,
req
,
res
);
},
200
)
},
200
)
}).
listen
(
8001
);
}).
listen
(
8001
);
sys
.
puts
(
'
http proxy server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8001
'
.
yellow
+
'
with latency
'
.
magenta
.
underline
);
sys
.
puts
(
'
http proxy server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8001
'
.
yellow
+
'
with latency
'
.
magenta
.
underline
);
...
@@ -58,7 +58,7 @@ http.createServer(function (req, res){
...
@@ -58,7 +58,7 @@ http.createServer(function (req, res){
proxy
.
watch
(
req
,
res
);
proxy
.
watch
(
req
,
res
);
setTimeout
(
function
(){
setTimeout
(
function
(){
proxy
.
proxyRequest
(
'
localhost
'
,
9000
,
req
,
res
);
proxy
.
proxyRequest
(
9000
,
'
localhost
'
,
req
,
res
);
},
200
);
},
200
);
}).
listen
(
8002
);
}).
listen
(
8002
);
sys
.
puts
(
'
http server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8002
'
.
yellow
+
'
with proxyRequest handler
'
.
cyan
.
underline
+
'
and latency
'
.
magenta
);
sys
.
puts
(
'
http server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8002
'
.
yellow
+
'
with proxyRequest handler
'
.
cyan
.
underline
+
'
and latency
'
.
magenta
);
...
...
lib/node-http-proxy.js
View file @
bde98f48
...
@@ -61,8 +61,8 @@ exports.HttpProxy.prototype = {
...
@@ -61,8 +61,8 @@ exports.HttpProxy.prototype = {
callback
=
arguments
[
0
];
callback
=
arguments
[
0
];
}
}
else
{
else
{
server
=
arguments
[
0
];
port
=
arguments
[
0
];
port
=
arguments
[
1
];
server
=
arguments
[
1
];
}
}
var
proxyServer
=
http
.
createServer
(
function
(
req
,
res
){
var
proxyServer
=
http
.
createServer
(
function
(
req
,
res
){
...
...
test/node-http-proxy-test.js
View file @
bde98f48
...
@@ -37,8 +37,8 @@ var testServers = {};
...
@@ -37,8 +37,8 @@ var testServers = {};
//
//
// Creates the reverse proxy server
// Creates the reverse proxy server
//
//
var
startProxyServer
=
function
(
server
,
port
,
proxy
)
{
var
startProxyServer
=
function
(
port
,
server
,
proxy
)
{
var
proxyServer
=
proxy
.
createServer
(
server
,
port
);
var
proxyServer
=
proxy
.
createServer
(
port
,
server
);
proxyServer
.
listen
(
8080
);
proxyServer
.
listen
(
8080
);
return
proxyServer
;
return
proxyServer
;
};
};
...
@@ -50,7 +50,7 @@ var startLatentProxyServer = function (server, port, proxy, latency) {
...
@@ -50,7 +50,7 @@ var startLatentProxyServer = function (server, port, proxy, latency) {
// Initialize the nodeProxy and start proxying the request
// Initialize the nodeProxy and start proxying the request
var
proxyServer
=
proxy
.
createServer
(
function
(
req
,
res
,
proxy
)
{
var
proxyServer
=
proxy
.
createServer
(
function
(
req
,
res
,
proxy
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
proxy
.
proxyRequest
(
server
,
port
,
req
,
res
);
proxy
.
proxyRequest
(
port
,
server
,
req
,
res
);
},
latency
);
},
latency
);
});
});
...
@@ -77,7 +77,7 @@ var startTargetServer = function (port) {
...
@@ -77,7 +77,7 @@ var startTargetServer = function (port) {
//
//
var
startTest
=
function
(
proxy
,
port
)
{
var
startTest
=
function
(
proxy
,
port
)
{
testServers
.
noLatency
=
[];
testServers
.
noLatency
=
[];
testServers
.
noLatency
.
push
(
startProxyServer
(
'
localhost
'
,
port
,
proxy
));
testServers
.
noLatency
.
push
(
startProxyServer
(
port
,
'
localhost
'
,
proxy
));
testServers
.
noLatency
.
push
(
startTargetServer
(
port
));
testServers
.
noLatency
.
push
(
startTargetServer
(
port
));
};
};
...
@@ -86,7 +86,7 @@ var startTest = function (proxy, port) {
...
@@ -86,7 +86,7 @@ var startTest = function (proxy, port) {
//
//
var
startTestWithLatency
=
function
(
proxy
,
port
)
{
var
startTestWithLatency
=
function
(
proxy
,
port
)
{
testServers
.
latency
=
[];
testServers
.
latency
=
[];
testServers
.
latency
.
push
(
startLatentProxyServer
(
'
localhost
'
,
port
,
proxy
,
2000
));
testServers
.
latency
.
push
(
startLatentProxyServer
(
port
,
'
localhost
'
,
proxy
,
2000
));
testServers
.
latency
.
push
(
startTargetServer
(
port
));
testServers
.
latency
.
push
(
startTargetServer
(
port
));
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment