Skip to content

Commit 0393b5d

Browse files
committed
[docs] more short examples to the Readme
1 parent ae0faef commit 0393b5d

File tree

1 file changed

+118
-3
lines changed

1 file changed

+118
-3
lines changed

README.md

+118-3
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ http.createServer(function (req, res) {
8686

8787
#### Setup a stand-alone proxy server with custom server logic
8888
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.
89+
and also you can put your own logic to hanlde the request.
9190

92-
``` js
91+
```js
9392
var http = require('http'),
9493
httpProxy = require('http-proxy');
9594

@@ -147,6 +146,122 @@ http.createServer(function (req, res) {
147146
}).listen(9008);
148147
```
149148

149+
#### Listening for proxy events
150+
151+
* `error`: The error event is emitted if the request to the target fail.
152+
* `proxyRes`: This event is emitted if the request to the target got a response.
153+
154+
```js
155+
var httpProxy = require('http-proxy');
156+
// Error example
157+
//
158+
// Http Proxy Server with bad target
159+
//
160+
var proxy = httpProxy.createServer({
161+
target:'http://localhost:9005'
162+
});
163+
164+
//
165+
// Tell the proxy to listen on port 8000
166+
//
167+
proxy.listen(8005);
168+
169+
//
170+
// Listen for the `error` event on `proxy`.
171+
proxy.on('error', function (err, req, res) {
172+
res.writeHead(500, {
173+
'Content-Type': 'text/plain'
174+
});
175+
176+
res.end('Something went wrong. And we are reporting a custom error message.');
177+
});
178+
179+
//
180+
// Listen for the `proxyRes` event on `proxy`.
181+
//
182+
proxy.on('proxyRes', function (res) {
183+
console.log('RAW Response from the target', res.headers);
184+
});
185+
186+
```
187+
188+
#### Using HTTPS
189+
You can activate the validation of a secure SSL certificate to the target connection (avoid self signed certs), just set `secure: true` in the options.
190+
191+
##### HTTPS -> HTTP
192+
193+
```js
194+
//
195+
// Create the HTTPS proxy server in front of a HTTP server
196+
//
197+
httpProxy.createServer({
198+
target: {
199+
host: 'localhost',
200+
port: 9009
201+
},
202+
ssl: {
203+
key: fs.readFileSync('valid-ssl-key.pem'), 'utf8'),
204+
cert: fs.readFileSync('valid-ssl-cert.pem'), 'utf8')
205+
}
206+
}).listen(8009);
207+
```
208+
209+
##### HTTPS -> HTTPS
210+
211+
```js
212+
//
213+
// Create the proxy server listening on port 443
214+
//
215+
httpProxy.createServer({
216+
ssl: {
217+
key: fs.readFileSync('valid-ssl-key.pem'), 'utf8'),
218+
cert: fs.readFileSync('valid-ssl-cert.pem'), 'utf8')
219+
},
220+
target: 'https://localhost:9010',
221+
secure: true // Depends on your needs, could be false.
222+
}).listen(443);
223+
```
224+
225+
#### Proxying WebSockets
226+
You can activate the websocket support for the proxy using `ws:true` in the options.
227+
228+
```js
229+
//
230+
// Create a proxy server for websockets
231+
//
232+
httpProxy.createServer({
233+
target: 'ws://localhost:9014',
234+
ws: true
235+
}).listen(8014);
236+
```
237+
238+
Also you can proxy the websocket requests just calling the `ws(req, socket, head)` method.
239+
240+
```js
241+
//
242+
// Setup our server to proxy standard HTTP requests
243+
//
244+
var proxy = new httpProxy.createProxyServer({
245+
target: {
246+
host: 'localhost',
247+
port: 9015
248+
}
249+
});
250+
var proxyServer = http.createServer(function (req, res) {
251+
proxy.web(req, res);
252+
});
253+
254+
//
255+
// Listen to the `upgrade` event and proxy the
256+
// WebSocket requests as well.
257+
//
258+
proxyServer.on('upgrade', function (req, socket, head) {
259+
proxy.ws(req, socket, head);
260+
});
261+
262+
proxyServer.listen(8015);
263+
```
264+
150265
### Contributing and Issues
151266

152267
* Search on Google/Github

0 commit comments

Comments
 (0)