|
1 |
| -`caronte` is a from-scratch implementation of `http-proxy` and, as such |
| 1 | +Looking to upgrade from `[email protected]` to `[email protected]`? You've come to the right place! |
| 2 | +`[email protected]` is a from-scratch implementation of `http-proxy` and, as such |
2 | 3 | brings some breaking changes to APIs.
|
3 | 4 |
|
4 | 5 | ## Server creation
|
5 | 6 |
|
6 | 7 | Available through `.createServer()` or `.createProxyServer()`.
|
7 |
| -Check the README.md for a more detailed explanation of the parameters. |
| 8 | + |
| 9 | +```javascript |
| 10 | +httpProxy.createServer({ |
| 11 | + target:'http://localhost:9003' |
| 12 | +}).listen(8003); |
| 13 | +``` |
| 14 | + |
| 15 | +Check the [README.md](https://github.com/nodejitsu/node-http-proxy/blob/caronte/README.md) for a more detailed explanation of the parameters. |
8 | 16 |
|
9 | 17 | ## Proxying
|
10 | 18 |
|
11 |
| -Web proying is done by calling the `.web()` method on a Proxy instance. Websockets |
12 |
| -are proxied by the `.ws()` method. |
| 19 | +Web proying is done by calling the `.web()` method on a Proxy instance. You can check among some use cases in the [examples folder](https://github.com/nodejitsu/node-http-proxy/tree/caronte/examples/http) |
| 20 | + |
| 21 | +```javascript |
| 22 | +// |
| 23 | +// Create a HTTP Proxy server with a HTTPS target |
| 24 | +// |
| 25 | +httpProxy.createProxyServer({ |
| 26 | + target: 'https://google.com', |
| 27 | + agent : https.globalAgent, |
| 28 | + headers: { |
| 29 | + host: 'google.com' |
| 30 | + } |
| 31 | +}).listen(8011); |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | +Websockets are proxied by the `.ws()` method. The [examples folder](https://github.com/nodejitsu/node-http-proxy/tree/caronte/examples/websocket) again provides a lot of useful snippets! |
| 36 | + |
| 37 | +```javascript |
| 38 | +var proxy = new httpProxy.createProxyServer({ |
| 39 | + target: { |
| 40 | + host: 'localhost', |
| 41 | + port: 9015 |
| 42 | + } |
| 43 | +}); |
| 44 | +var proxyServer = http.createServer(function (req, res) { |
| 45 | + proxy.web(req, res); |
| 46 | +}); |
| 47 | + |
| 48 | +// |
| 49 | +// Listen to the `upgrade` event and proxy the |
| 50 | +// WebSocket requests as well. |
| 51 | +// |
| 52 | +proxyServer.on('upgrade', function (req, socket, head) { |
| 53 | + proxy.ws(req, socket, head); |
| 54 | +}); |
| 55 | +``` |
13 | 56 |
|
14 | 57 | ## Error Handling
|
15 | 58 |
|
16 | 59 | It is possible to listen globally on the `error` event on the server. In alternative, a
|
17 | 60 | callback passed to `.web()` or `.ws()` as last parameter is also accepted.
|
18 | 61 |
|
| 62 | +```javascript |
| 63 | +var proxy = httpProxy.createServer({ |
| 64 | + target:'http://localhost:9005' |
| 65 | +}); |
| 66 | + |
| 67 | +// |
| 68 | +// Tell the proxy to listen on port 8000 |
| 69 | +// |
| 70 | +proxy.listen(8005); |
| 71 | + |
| 72 | +// |
| 73 | +// Listen for the `error` event on `proxy`. |
| 74 | +proxy.on('error', function (err, req, res) { |
| 75 | + res.writeHead(500, { |
| 76 | + 'Content-Type': 'text/plain' |
| 77 | + }); |
| 78 | + |
| 79 | + res.end('Something went wrong. And we are reporting a custom error message.'); |
| 80 | +}); |
| 81 | +``` |
| 82 | + |
19 | 83 | ## Dropped
|
20 | 84 |
|
21 | 85 | Since the API was rewritten to be extremely flexible we decided to drop some features
|
22 |
| -which were in the core and delegate them to eventual "user-land" modules. |
| 86 | +which were in the core and delegate them to eventual "userland" modules. |
23 | 87 |
|
24 | 88 | - Middleware API
|
25 | 89 | - ProxyTable API
|
26 | 90 |
|
| 91 | +### Middleware API |
| 92 | + |
| 93 | +The new API makes it really easy to implement code that behaves like the old Middleware API. You can check some examples [here](https://github.com/nodejitsu/node-http-proxy/tree/caronte/examples/middleware) |
| 94 | + |
| 95 | + |
| 96 | + |
0 commit comments