From 9a7e4a1ac9c1923fa662e5ba14ccc0a729104614 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 24 Sep 2018 12:01:33 +0200 Subject: [PATCH 1/2] Added keepProxy to not rewrite the URL. --- README.md | 8 ++++++++ index.js | 6 +++++- test.js | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d7874bd..6c87268 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,14 @@ An URL (including protocol) that represents the target server to use for proxyin The prefix to mount this plugin on. All the requests to the current server starting with the given prefix will be proxied to the provided upstream. +The prefix will be removed from the URL when forwarding the HTTP +request. + +### keepPrefix + +Set it to `true` to keep the prefix this plugin is mounted to when +forwarding the HTTP requests. + ### beforeHandler A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication). diff --git a/index.js b/index.js index 5f0c188..a4380f8 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ module.exports = async function (fastify, opts) { } const beforeHandler = opts.beforeHandler + const removePrefix = !opts.keepPrefix const fromOpts = Object.assign({}, opts) fromOpts.base = opts.upstream @@ -26,7 +27,10 @@ module.exports = async function (fastify, opts) { fastify.all('/*', { beforeHandler }, reply) function reply (request, reply) { - var dest = request.req.url.replace(this.basePath, '') + var dest = request.req.url + if (removePrefix) { + dest = dest.replace(this.basePath, '') + } reply.from(dest || '/', opts.replyOptions) } } diff --git a/test.js b/test.js index 5ae4943..91e7c61 100644 --- a/test.js +++ b/test.js @@ -23,6 +23,10 @@ async function run () { throw new Error('kaboom') }) + origin.get('/api/a', async (request, reply) => { + return 'this is /api/a' + }) + await origin.listen(0) tearDown(origin.close.bind(origin)) @@ -179,6 +183,25 @@ async function run () { const { headers } = await got(`http://localhost:${proxyServer.server.address().port}/api`) t.match(headers, { 'x-test': 'test' }) }) + + test('keepPrefix true', async (t) => { + const proxyServer = Fastify() + + proxyServer.register(proxy, { + upstream: `http://localhost:${origin.server.address().port}`, + prefix: '/api', + keepPrefix: true + }) + + await proxyServer.listen(0) + + t.tearDown(() => { + proxyServer.close() + }) + + const firstProxyPrefix = await got(`http://localhost:${proxyServer.server.address().port}/api/a`) + t.equal(firstProxyPrefix.body, 'this is /api/a') + }) } run() From 44dd788398f1a378d62270cfd15c67c4d1ae8fda Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 24 Sep 2018 23:42:23 +0100 Subject: [PATCH 2/2] keepPrefix -> rewritePrefix --- README.md | 5 ++--- index.js | 6 ++---- test.js | 8 ++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6c87268..2e10a59 100644 --- a/README.md +++ b/README.md @@ -82,10 +82,9 @@ The prefix to mount this plugin on. All the requests to the current server start The prefix will be removed from the URL when forwarding the HTTP request. -### keepPrefix +### rewritePrefix -Set it to `true` to keep the prefix this plugin is mounted to when -forwarding the HTTP requests. +Rewrite the prefix to the specified string. Default: `''`. ### beforeHandler diff --git a/index.js b/index.js index a4380f8..d12f75c 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ module.exports = async function (fastify, opts) { } const beforeHandler = opts.beforeHandler - const removePrefix = !opts.keepPrefix + const rewritePrefix = opts.rewritePrefix || '' const fromOpts = Object.assign({}, opts) fromOpts.base = opts.upstream @@ -28,9 +28,7 @@ module.exports = async function (fastify, opts) { function reply (request, reply) { var dest = request.req.url - if (removePrefix) { - dest = dest.replace(this.basePath, '') - } + dest = dest.replace(this.basePath, rewritePrefix) reply.from(dest || '/', opts.replyOptions) } } diff --git a/test.js b/test.js index 91e7c61..c125a73 100644 --- a/test.js +++ b/test.js @@ -23,8 +23,8 @@ async function run () { throw new Error('kaboom') }) - origin.get('/api/a', async (request, reply) => { - return 'this is /api/a' + origin.get('/api2/a', async (request, reply) => { + return 'this is /api2/a' }) await origin.listen(0) @@ -190,7 +190,7 @@ async function run () { proxyServer.register(proxy, { upstream: `http://localhost:${origin.server.address().port}`, prefix: '/api', - keepPrefix: true + rewritePrefix: '/api2' }) await proxyServer.listen(0) @@ -200,7 +200,7 @@ async function run () { }) const firstProxyPrefix = await got(`http://localhost:${proxyServer.server.address().port}/api/a`) - t.equal(firstProxyPrefix.body, 'this is /api/a') + t.equal(firstProxyPrefix.body, 'this is /api2/a') }) }