Skip to content

Commit 8ab3717

Browse files
author
Pavel Kovalenko
committed
preValidation docs fix
1 parent 4a34ad7 commit 8ab3717

File tree

1 file changed

+51
-46
lines changed

1 file changed

+51
-46
lines changed

README.md

+51-46
Original file line numberDiff line numberDiff line change
@@ -27,58 +27,58 @@ npm i @fastify/http-proxy fastify
2727
## Example
2828

2929
```js
30-
const Fastify = require('fastify')
31-
const server = Fastify()
30+
const Fastify = require('fastify');
31+
const server = Fastify();
3232

3333
server.register(require('@fastify/http-proxy'), {
3434
upstream: 'http://my-api.example.com',
3535
prefix: '/api', // optional
36-
http2: false // optional
37-
})
36+
http2: false, // optional
37+
});
3838

39-
server.listen({ port: 3000 })
39+
server.listen({ port: 3000 });
4040
```
4141

4242
This will proxy any request starting with `/api` to `http://my-api.example.com`. For instance `http://localhost:3000/api/users` will be proxied to `http://my-api.example.com/users`.
4343

4444
If you want to have different proxies on different prefixes you can register multiple instances of the plugin as shown in the following snippet:
4545

4646
```js
47-
const Fastify = require('fastify')
48-
const server = Fastify()
49-
const proxy = require('@fastify/http-proxy')
47+
const Fastify = require('fastify');
48+
const server = Fastify();
49+
const proxy = require('@fastify/http-proxy');
5050

5151
// /api/x will be proxied to http://my-api.example.com/x
5252
server.register(proxy, {
5353
upstream: 'http://my-api.example.com',
5454
prefix: '/api', // optional
55-
http2: false // optional
56-
})
55+
http2: false, // optional
56+
});
5757

5858
// /rest-api/123/endpoint will be proxied to http://my-rest-api.example.com/123/endpoint
5959
server.register(proxy, {
6060
upstream: 'http://my-rest-api.example.com',
6161
prefix: '/rest-api/:id/endpoint', // optional
6262
rewritePrefix: '/:id/endpoint', // optional
63-
http2: false // optional
64-
})
63+
http2: false, // optional
64+
});
6565

6666
// /auth/user will be proxied to http://single-signon.example.com/signon/user
6767
server.register(proxy, {
6868
upstream: 'http://single-signon.example.com',
6969
prefix: '/auth', // optional
7070
rewritePrefix: '/signon', // optional
71-
http2: false // optional
72-
})
71+
http2: false, // optional
72+
});
7373

7474
// /user will be proxied to http://single-signon.example.com/signon/user
7575
server.register(proxy, {
7676
upstream: 'http://single-signon.example.com',
7777
rewritePrefix: '/signon', // optional
78-
http2: false // optional
79-
})
78+
http2: false, // optional
79+
});
8080

81-
server.listen({ port: 3000 })
81+
server.listen({ port: 3000 });
8282
```
8383

8484
Notice that in this case it is important to use the `prefix` option to tell the proxy how to properly route the requests across different upstreams.
@@ -92,20 +92,22 @@ For other examples, see [`example.js`](examples/example.js).
9292
`@fastify/http-proxy` can track and pipe the `request-id` across the upstreams. Using the [`hyperid`](https://www.npmjs.com/package/hyperid) module and the [`@fastify/reply-from`](https://github.com/fastify/fastify-reply-from) built-in options a fairly simple example would look like this:
9393

9494
```js
95-
const Fastify = require('fastify')
96-
const proxy = require('@fastify/http-proxy')
97-
const hyperid = require('hyperid')
95+
const Fastify = require('fastify');
96+
const proxy = require('@fastify/http-proxy');
97+
const hyperid = require('hyperid');
9898

99-
const server = Fastify()
100-
const uuid = hyperid()
99+
const server = Fastify();
100+
const uuid = hyperid();
101101

102102
server.register(proxy, {
103103
upstream: 'http://localhost:4001',
104104
replyOptions: {
105-
rewriteRequestHeaders: (originalReq, headers) => ({...headers, 'request-id': uuid()})
106-
}
107-
})
108-
105+
rewriteRequestHeaders: (originalReq, headers) => ({
106+
...headers,
107+
'request-id': uuid(),
108+
}),
109+
},
110+
});
109111

110112
server.listen({ port: 3000 });
111113
```
@@ -115,8 +117,8 @@ server.listen({ port: 3000 });
115117
This `fastify` plugin supports _all_ the options of
116118
[`@fastify/reply-from`](https://github.com/fastify/fastify-reply-from) plus the following.
117119

118-
*Note that this plugin is fully encapsulated, and non-JSON payloads will
119-
be streamed directly to the destination.*
120+
_Note that this plugin is fully encapsulated, and non-JSON payloads will
121+
be streamed directly to the destination._
120122

121123
### `upstream`
122124

@@ -147,9 +149,9 @@ For example, if you are expecting a payload of type `application/xml`, then you
147149

148150
```javascript
149151
fastify.addContentTypeParser('application/xml', (req, done) => {
150-
const parsedBody = parsingCode(req)
151-
done(null, parsedBody)
152-
})
152+
const parsedBody = parsingCode(req);
153+
done(null, parsedBody);
154+
});
153155
```
154156

155157
### `preValidation`
@@ -158,13 +160,13 @@ Specify preValidation function to perform the validation of the request before t
158160

159161
```javascript
160162
fastify.register(proxy, {
161-
upstream: `http://your-target-upstream.com`,
162-
preValidation: async (request, reply) => {
163-
if (request.body.method !== 'invalid_method') {
164-
reply.code(400).send({ message: 'payload contains invalid method' })
165-
}
166-
}
167-
})
163+
upstream: `http://your-target-upstream.com`,
164+
preValidation: async (request, reply) => {
165+
if (request.body.method === 'invalid_method') {
166+
reply.code(400).send({ message: 'payload contains invalid method' });
167+
}
168+
},
169+
});
168170
```
169171

170172
### `config`
@@ -179,6 +181,7 @@ configuration passed to the route.
179181
Object with [reply options](https://github.com/fastify/fastify-reply-from#replyfromsource-opts) for `@fastify/reply-from`.
180182

181183
### `internalRewriteLocationHeader`
184+
182185
By default, `@fastify/http-proxy` will rewrite the `location` header when a request redirects to a relative path.
183186
In other words, the [prefix](https://github.com/fastify/fastify-http-proxy#prefix) will be added to the relative path.
184187

@@ -187,12 +190,13 @@ If you want to preserve the original path, this option will disable this interna
187190
Note that the [rewriteHeaders](https://github.com/fastify/fastify-reply-from#rewriteheadersheaders-request) option of [`@fastify/reply-from`](http://npm.im/fastify-reply-from) will retrieve headers modified (reminder: only `location` is updated among all headers) in parameter but with this option, the headers are unchanged.
188191

189192
### `httpMethods`
193+
190194
An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.
191195

192196
### `websocket`
193197

194198
This module has _partial_ support for forwarding websockets by passing a
195-
`websocket` boolean option.
199+
`websocket` boolean option.
196200

197201
A few things are missing:
198202

@@ -203,6 +207,7 @@ A few things are missing:
203207
Pull requests are welcome to finish this feature.
204208

205209
### `wsUpstream`
210+
206211
Working only if property `websocket` is `true`.
207212

208213
An URL (including protocol) that represents the target websockets to use for proxying websockets.
@@ -229,19 +234,19 @@ The default implementation forwards the `cookie` header.
229234

230235
The following benchmarks where generated on a dedicated server with an Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz and 64GB of RAM:
231236

232-
| __Framework__ | req/sec |
233-
| :----------------- | :------------------------- |
234-
| `express-http-proxy` | 2557 |
235-
| `http-proxy` | 9519 |
236-
| `@fastify/http-proxy` | 15919 |
237+
| **Framework** | req/sec |
238+
| :-------------------- | :------ |
239+
| `express-http-proxy` | 2557 |
240+
| `http-proxy` | 9519 |
241+
| `@fastify/http-proxy` | 15919 |
237242

238243
The results were gathered on the second run of `autocannon -c 100 -d 5
239244
URL`.
240245

241246
## TODO
242247

243-
* [ ] Perform validations for incoming data
244-
* [ ] Finish implementing websocket
248+
- [ ] Perform validations for incoming data
249+
- [ ] Finish implementing websocket
245250

246251
## License
247252

0 commit comments

Comments
 (0)