Skip to content

Commit ae0faef

Browse files
committed
[docs] Update readme with more how to
1 parent 584ce76 commit ae0faef

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

README.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ to the client.
7070
var http = require('http'),
7171
httpProxy = require('http-proxy');
7272
//
73-
// Create your proxy server
73+
// Create your proxy server and set the target in the options.
7474
//
7575
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000);
7676

@@ -85,6 +85,9 @@ http.createServer(function (req, res) {
8585
```
8686

8787
#### Setup a stand-alone proxy server with custom server logic
88+
This example show how you can proxy a request using your own HTTP server
89+
and also you can put your own logic to hanlde the request. This example
90+
could show how to proxy requests within another http server.
8891

8992
``` js
9093
var http = require('http'),
@@ -95,14 +98,55 @@ var http = require('http'),
9598
//
9699
var proxy = httpProxy.createProxyServer({});
97100

101+
//
102+
// Create your custom server and just call `proxy.web()` to proxy
103+
// a web request to the target passed in the options
104+
// also you can use `proxy.ws()` to proxy a websockets request
105+
//
98106
var server = require('http').createServer(function(req, res) {
107+
// You can define here your custom logic to handle the request
108+
// and then proxy the request.
99109
proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
100110
});
101111

102112
console.log("listening on port 5050")
103113
server.listen(5050);
104114
```
105115

116+
#### Setup a stand-alone proxy server with latency
117+
118+
```
119+
var http = require('http'),
120+
httpProxy = require('http-proxy');
121+
122+
//
123+
// Create a proxy server with latency
124+
//
125+
var proxy = httpProxy.createProxyServer();
126+
127+
//
128+
// Create your server that make an operation that take a while
129+
// and then proxy de request
130+
//
131+
http.createServer(function (req, res) {
132+
// This simulate an operation that take 500ms in execute
133+
setTimeout(function () {
134+
proxy.web(req, res, {
135+
target: 'http://localhost:9008'
136+
});
137+
}, 500);
138+
}).listen(8008);
139+
140+
//
141+
// Create your target server
142+
//
143+
http.createServer(function (req, res) {
144+
res.writeHead(200, { 'Content-Type': 'text/plain' });
145+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
146+
res.end();
147+
}).listen(9008);
148+
```
149+
106150
### Contributing and Issues
107151

108152
* Search on Google/Github
@@ -118,6 +162,7 @@ server.listen(5050);
118162
* **target**: url string to be parsed with the url module
119163
* **forward**: url string to be parsed with the url module
120164
* **agent**: object to be passed to http(s).request (see Node's [https agent](http://nodejs.org/api/https.html#https_class_https_agent) and [http agent](http://nodejs.org/api/http.html#http_class_http_agent) objects)
165+
* **secure**: true/false, if you want to verify the SSL Certs
121166

122167
If you are using the `proxyServer.listen` method, the following options are also applicable:
123168

0 commit comments

Comments
 (0)