Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ See [Config](https://fastify.dev/docs/latest/Reference/Routes/#config) in the Fa
documentation for information on this option. Note: this is merged with other
configuration passed to the route.

### `constraints`

The [route constraints](https://fastify.dev/docs/latest/Reference/Routes/#constraints) applied to the proxied routes. Default: `{}`.

```js
// only requests for api.example.com are proxied
server.register(proxy, {
upstream: 'http://my-api.example.com',
constraints: { host: 'api.example.com' }
})
```

### `replyOptions`

Object with [reply options](https://github.com/fastify/fastify-reply-from#replyfromsource-opts) for `@fastify/reply-from`.
Expand All @@ -212,7 +224,7 @@ Note that the [rewriteHeaders](https://github.com/fastify/fastify-reply-from#rew

### `httpMethods`

An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.
An HTTP method or an array of HTTP methods to proxy. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.

Methods that are already registered for one of the proxied routes are skipped (a `debug` log entry lists the skipped methods). This allows plugins such as [`@fastify/cors`](https://github.com/fastify/fastify-cors), which registers an `OPTIONS /*` route to answer preflight requests, to be used together with this plugin. Such plugins must be registered **before** `@fastify/http-proxy`:

Expand All @@ -229,6 +241,17 @@ If you register them in the opposite order, remove `OPTIONS` from `httpMethods`

An array that contains the routes to handle. Default: `['/', '/*']`.

The default values of `httpMethods` and `routes` are exported as `defaultHttpMethods` and `defaultRoutes`, so they can be extended instead of copied:

```js
const proxy = require('@fastify/http-proxy')

server.register(proxy, {
upstream: 'http://my-api.example.com',
httpMethods: [...proxy.defaultHttpMethods, 'TRACE']
})
```

### `preRewrite`

A function that will be executed before rewriting the URL. It receives the URL, the request parameters and the prefix and must return the new URL.
Expand Down Expand Up @@ -283,6 +306,20 @@ It also supports an additional `rewriteRequestHeaders(headers, request)` functio
opening the WebSocket connection. This function should return an object with the given headers.
The default implementation forwards the `cookie` header.

It also supports a `queryString` option to replace the query string sent to the upstream, which by default is the one
of the client request. It can be an object, or a function `queryString(search, reqUrl, request)` returning the new
query string without the leading `?` (`search` is the original one, including the leading `?`).

```js
server.register(proxy, {
upstream: 'http://my-api.example.com',
websocket: true,
wsClientOptions: {
queryString: { token: 'abc' }
}
})
```

The client's `Origin` header is always forwarded to the upstream, so that the upstream can enforce its own
origin checks (e.g. against cross-site WebSocket hijacking). This happens regardless of `rewriteRequestHeaders`,
unless an `origin` option is set in `wsClientOptions` or the rewritten headers already contain an `Origin` header.
Expand Down