Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keepProxy to not rewrite the URL. #24

Merged
merged 2 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ 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.

### rewritePrefix

Rewrite the prefix to the specified string. Default: `''`.

### beforeHandler

A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = async function (fastify, opts) {
}

const beforeHandler = opts.beforeHandler
const rewritePrefix = opts.rewritePrefix || ''

const fromOpts = Object.assign({}, opts)
fromOpts.base = opts.upstream
Expand All @@ -26,7 +27,8 @@ 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
dest = dest.replace(this.basePath, rewritePrefix)
reply.from(dest || '/', opts.replyOptions)
}
}
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ async function run () {
throw new Error('kaboom')
})

origin.get('/api2/a', async (request, reply) => {
return 'this is /api2/a'
})

await origin.listen(0)

tearDown(origin.close.bind(origin))
Expand Down Expand Up @@ -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',
rewritePrefix: '/api2'
})

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 /api2/a')
})
}

run()