@@ -8,7 +8,8 @@ const fp = require('fastify-plugin')
88const qs = require ( 'fast-querystring' )
99const { 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' ]
1213const urlPattern = / ^ h t t p s ? : \/ \/ /
1314const kWs = Symbol ( 'ws' )
1415const kWsHead = Symbol ( 'wsHead' )
@@ -82,6 +83,10 @@ function isExternalUrl (url) {
8283
8384function noop ( ) { }
8485
86+ function noopPreRewrite ( url ) {
87+ return url
88+ }
89+
8590function createContext ( logger ) {
8691 return { log : logger }
8792}
@@ -518,6 +523,7 @@ async function fastifyHttpProxy (fastify, opts) {
518523 opts = validateOptions ( opts )
519524
520525 const preHandler = opts . preHandler || opts . beforeHandler
526+ const preRewrite = typeof opts . preRewrite === 'function' ? opts . preRewrite : noopPreRewrite
521527 const rewritePrefix = generateRewritePrefix ( fastify . prefix , opts )
522528
523529 const fromOpts = Object . assign ( { } , opts )
@@ -555,22 +561,13 @@ async function fastifyHttpProxy (fastify, opts) {
555561 done ( null , payload )
556562 }
557563
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- } )
564+ const method = opts . httpMethods || defaultHttpMethods
565+ const config = opts . config || { }
566+ const constraints = opts . constraints || { }
567+
568+ for ( const url of opts . routes || defaultRoutes ) {
569+ fastify . route ( { url, method, preHandler, config, constraints, handler } )
570+ }
574571
575572 let wsProxy
576573
@@ -593,6 +590,8 @@ async function fastifyHttpProxy (fastify, opts) {
593590 }
594591
595592 function fromParameters ( url , params = { } , prefix = '/' ) {
593+ url = preRewrite ( url , params , prefix )
594+
596595 const { path, queryParams } = extractUrlComponents ( url )
597596 let dest = path
598597
@@ -646,4 +645,6 @@ module.exports = fp(fastifyHttpProxy, {
646645 encapsulate : true
647646} )
648647module . exports . default = fastifyHttpProxy
648+ module . exports . defaultRoutes = defaultRoutes
649+ module . exports . defaultHttpMethods = defaultHttpMethods
649650module . exports . fastifyHttpProxy = fastifyHttpProxy
0 commit comments