|
| 1 | +# fast-proxy-lite |
| 2 | +Node.js framework agnostic library that enables you to forward an http request to another HTTP server. |
| 3 | +Supported proxy protocols: HTTP, HTTPS |
| 4 | + |
| 5 | +> This library was initially forked from `fast-proxy`: https://github.com/fastify/fast-proxy |
| 6 | +
|
| 7 | +`fast-proxy-lite` powers: https://www.npmjs.com/package/fast-gateway 🚀 |
| 8 | +## Install |
| 9 | +``` |
| 10 | +npm i fast-proxy-lite |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | +The following examples describe how to use `fast-proxy-lite` with `restana`: |
| 15 | + |
| 16 | +Gateway: |
| 17 | +```js |
| 18 | +const { proxy, close } = require('fast-proxy-lite')({ |
| 19 | + base: 'http://127.0.0.1:3000' |
| 20 | + // options |
| 21 | +}) |
| 22 | +const gateway = require('restana')() |
| 23 | + |
| 24 | +gateway.all('/service/*', function (req, res) { |
| 25 | + proxy(req, res, req.url, {}) |
| 26 | +}) |
| 27 | + |
| 28 | +gateway.start(8080) |
| 29 | +``` |
| 30 | + |
| 31 | +Remote service: |
| 32 | +```js |
| 33 | +const service = require('restana')() |
| 34 | +service.get('/service/hi', (req, res) => res.send('Hello World!')) |
| 35 | + |
| 36 | +service.start(3000) |
| 37 | +``` |
| 38 | + |
| 39 | +Using imports: |
| 40 | +```ts |
| 41 | +import fastProxy from 'fast-proxy-lite' |
| 42 | + |
| 43 | +const { proxy, close } = fastProxy({ |
| 44 | + base: 'http://127.0.0.1:3000' |
| 45 | +}) |
| 46 | +``` |
| 47 | + |
| 48 | +## Benchmarks |
| 49 | +Please see: https://github.com/jkyberneees/nodejs-proxy-benchmarks |
| 50 | + |
| 51 | +## API |
| 52 | + |
| 53 | +### Options |
| 54 | +#### `base` |
| 55 | +Set the base URL for all the forwarded requests. Will be required if `http2` is set to `true` |
| 56 | +Note that _path will be discarded_. |
| 57 | + |
| 58 | +#### cacheURLs |
| 59 | +The number of parsed URLs that will be cached. Default: 100. |
| 60 | +> Use value = `0` to disable the caching mechanism |
| 61 | +
|
| 62 | +#### requests.http and requests.https |
| 63 | +Allows to optionally overwrite the internal `http` and `https` client agents implementation. Defaults: [`http`](https://nodejs.org/api/http.html#http_http) and [`https`](https://nodejs.org/api/https.html#https_https). |
| 64 | + |
| 65 | +For example, this could be used to add support for following redirects, like so: |
| 66 | + |
| 67 | +```js |
| 68 | +... |
| 69 | + requests: { |
| 70 | + http: require('follow-redirects/http'), |
| 71 | + https: require('follow-redirects/https') |
| 72 | + } |
| 73 | +... |
| 74 | +``` |
| 75 | + |
| 76 | +#### keepAliveMsecs |
| 77 | +Defaults to 1 minute, passed down to [`http.Agent`][http-agent] and |
| 78 | +[`https.Agent`][https-agent] instances. |
| 79 | + |
| 80 | +#### maxSockets |
| 81 | +Defaults to 2048 sockets, passed down to [`http.Agent`][http-agent] and |
| 82 | +[`https.Agent`][https-agent] instances. |
| 83 | + |
| 84 | +#### rejectUnauthorized |
| 85 | +Defaults to `true`, passed down to [`https.Agent`][https-agent] instances. |
| 86 | +This needs to be set to `false` to reply from https servers with |
| 87 | +self-signed certificates. |
| 88 | + |
| 89 | +#### Extended configurations |
| 90 | +Other supported configurations in https://nodejs.org/api/http.html#http_new_agent_options can also be part of the `opts` object. |
| 91 | + |
| 92 | +### close |
| 93 | +Optional _"on `close` resource release"_ strategy. You can link this to your application shutdown hook as an example. |
| 94 | + |
| 95 | +### proxy(originReq, originRes, source, [opts]) |
| 96 | +Enables you to forward an http request to another HTTP server. |
| 97 | +```js |
| 98 | +proxy( |
| 99 | + originReq, // http.IncomingMessage |
| 100 | + originRes, // http.ServerResponse |
| 101 | + req.url, // String -> remote URL + path or path if base was set |
| 102 | + {} // Options described below |
| 103 | +) |
| 104 | +``` |
| 105 | +#### opts |
| 106 | + |
| 107 | +##### base |
| 108 | +Optionally indicates the base URL for the current request proxy. When used, the global `base` config is overwriten. |
| 109 | +> This configuration value is ignored when using HTTP2. |
| 110 | +
|
| 111 | +##### onResponse(req, res, stream) |
| 112 | +Called when an http response is received from the source. |
| 113 | +The default behavior is `pump(stream, res)`, which will be disabled if the |
| 114 | +option is specified. |
| 115 | + |
| 116 | +##### rewriteRequestHeaders(req, headers) |
| 117 | +Called to rewrite the headers of the request, before them being sent to the downstream server. |
| 118 | +It must return the new headers object. |
| 119 | + |
| 120 | +##### rewriteHeaders(headers) |
| 121 | +Called to rewrite the headers of the response, before them being copied |
| 122 | +over to the outer response. |
| 123 | +It must return the new headers object. |
| 124 | + |
| 125 | +##### request |
| 126 | +Extended options supported by `http[s].request` method (https://nodejs.org/api/http.html#http_http_request_options_callback) |
| 127 | +The following options are dynamically assigned: `method, port, path, hostname, headers, agent`. |
| 128 | + |
| 129 | +##### queryString |
| 130 | +Replaces the original querystring of the request with what is specified. |
| 131 | +This will get passed to |
| 132 | +[`querystring.stringify`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options). |
| 133 | + |
| 134 | +## Related topics |
| 135 | +- http-agent: https://nodejs.org/api/http.html#http_new_agent_options |
| 136 | +- https-agent: https://nodejs.org/api/https.html#https_class_https_agent |
| 137 | + |
| 138 | +## License |
| 139 | +MIT |
0 commit comments