Skip to content

Commit ddf11bf

Browse files
authored
Merge pull request #252 from StevenMatchett/master
Allow rewritePrefix without having to need prefix
2 parents d8d3c5f + 4be9c0c commit ddf11bf

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ server.register(proxy, {
6262
http2: false // optional
6363
})
6464

65+
// /user will be proxied to http://single-signon.example.com/signon/user
66+
server.register(proxy, {
67+
upstream: 'http://single-signon.example.com',
68+
rewritePrefix: '/signon', // optional
69+
http2: false // optional
70+
})
71+
6572
server.listen(3000)
6673
```
6774

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ function setupWebSocketProxy (fastify, options, rewritePrefix) {
120120
}
121121
}
122122

123-
function generateRewritePrefix (prefix, opts) {
124-
if (!prefix) {
125-
return ''
126-
}
127-
123+
function generateRewritePrefix (prefix = '', opts) {
128124
let rewritePrefix = opts.rewritePrefix || (opts.upstream ? new URL(opts.upstream).pathname : '/')
129125

130126
if (!prefix.endsWith('/') && rewritePrefix.endsWith('/')) {

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,26 @@ async function run () {
424424
t.equal(firstProxyPrefix.body, 'this is /api2/a')
425425
})
426426

427+
test('rewritePrefix without prefix', async t => {
428+
const proxyServer = Fastify()
429+
430+
proxyServer.register(proxy, {
431+
upstream: `http://localhost:${origin.server.address().port}`,
432+
rewritePrefix: '/api2'
433+
})
434+
435+
await proxyServer.listen({ port: 0 })
436+
437+
t.teardown(() => {
438+
proxyServer.close()
439+
})
440+
441+
const firstProxyPrefix = await got(
442+
`http://localhost:${proxyServer.server.address().port}/a`
443+
)
444+
t.equal(firstProxyPrefix.body, 'this is /api2/a')
445+
})
446+
427447
test('rewrite location headers', async t => {
428448
const proxyServer = Fastify()
429449

0 commit comments

Comments
 (0)