Skip to content

Commit a7f17bb

Browse files
committed
feat: Add routes option.
1 parent 9626251 commit a7f17bb

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ Note that the [rewriteHeaders](https://github.com/fastify/fastify-reply-from#rew
193193

194194
An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.
195195

196+
### `routes`
197+
198+
An array that contains the routes to handle. Default: `['/', '/*']`.
199+
196200
### `websocket`
197201

198202
This module has _partial_ support for forwarding websockets by passing a

index.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const fp = require('fastify-plugin')
88
const qs = require('fast-querystring')
99
const { validateOptions } = require('./src/options')
1010

11-
const httpMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
11+
const defaultRoutes = ['/', '/*']
12+
const defaultHttpMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
1213
const urlPattern = /^https?:\/\//
1314
const kWs = Symbol('ws')
1415
const kWsHead = Symbol('wsHead')
@@ -555,22 +556,13 @@ async function fastifyHttpProxy (fastify, opts) {
555556
done(null, payload)
556557
}
557558

558-
fastify.route({
559-
url: '/',
560-
method: opts.httpMethods || httpMethods,
561-
preHandler,
562-
config: opts.config || {},
563-
constraints: opts.constraints || {},
564-
handler
565-
})
566-
fastify.route({
567-
url: '/*',
568-
method: opts.httpMethods || httpMethods,
569-
preHandler,
570-
config: opts.config || {},
571-
constraints: opts.constraints || {},
572-
handler
573-
})
559+
const method = opts.httpMethods || defaultHttpMethods
560+
const config = opts.config || {}
561+
const constraints = opts.constraints || {}
562+
563+
for (const url of opts.routes || defaultRoutes) {
564+
fastify.route({ url, method, preHandler, config, constraints, handler })
565+
}
574566

575567
let wsProxy
576568

@@ -646,4 +638,6 @@ module.exports = fp(fastifyHttpProxy, {
646638
encapsulate: true
647639
})
648640
module.exports.default = fastifyHttpProxy
641+
module.exports.defaultRoutes = defaultRoutes
642+
module.exports.defaultHttpMethods = defaultHttpMethods
649643
module.exports.fastifyHttpProxy = fastifyHttpProxy

test/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,29 @@ async function run () {
751751
t.assert.fail()
752752
})
753753

754+
test('settings of routes', async t => {
755+
const server = Fastify()
756+
server.register(proxy, {
757+
upstream: `http://localhost:${origin.server.address().port}`,
758+
routes: ['/a']
759+
})
760+
761+
await server.listen({ port: 0 })
762+
t.after(() => server.close())
763+
764+
const resultRoot = await got(`http://localhost:${server.server.address().port}/a`)
765+
t.assert.deepStrictEqual(resultRoot.statusCode, 200)
766+
767+
let errored = false
768+
try {
769+
await await got(`http://localhost:${server.server.address().port}/api2/a`)
770+
} catch (err) {
771+
t.assert.strictEqual(err.response.statusCode, 404)
772+
errored = true
773+
}
774+
t.assert.ok(errored)
775+
})
776+
754777
test('settings of method types', async t => {
755778
const server = Fastify()
756779
server.register(proxy, {

0 commit comments

Comments
 (0)