|
| 1 | +# Connecting through a proxy |
| 2 | + |
| 3 | +Conneting through a proxy is possible by properly configuring the client constructor and request. |
| 4 | + |
| 5 | +The proxy url should be passed to the client constructor, while the upstream server url |
| 6 | +should be added to every request call in the `path`. |
| 7 | +For instance, if you need to send a request to the `/hello` route of your upstream server, |
| 8 | +the `path` should be `path: 'http://upstream.server:port/hello?foo=bar'`. |
| 9 | + |
| 10 | +If you proxy requires basic authentication, you can send it via the `proxy-authorization` header. |
| 11 | + |
| 12 | +### Connect without authentication |
| 13 | + |
| 14 | +```js |
| 15 | +import { Client } from 'undici' |
| 16 | +import { createServer } from 'http' |
| 17 | +import proxy = from 'proxy' |
| 18 | + |
| 19 | +const server = await buildServer() |
| 20 | +const proxy = await buildProxy() |
| 21 | + |
| 22 | +const serverUrl = `http://localhost:${server.address().port}` |
| 23 | +const proxyUrl = `http://localhost:${proxy.address().port}` |
| 24 | + |
| 25 | +server.on('request', (req, res) => { |
| 26 | + console.log(req.url) // '/hello?foo=bar' |
| 27 | + res.setHeader('content-type', 'application/json') |
| 28 | + res.end(JSON.stringify({ hello: 'world' })) |
| 29 | +}) |
| 30 | + |
| 31 | +const client = new Client(proxyUrl) |
| 32 | + |
| 33 | +const response = await client.request({ |
| 34 | + method: 'GET', |
| 35 | + path: serverUrl + '/hello?foo=bar' |
| 36 | +}) |
| 37 | + |
| 38 | +response.body.setEncoding('utf8') |
| 39 | +let data = '' |
| 40 | +for await (const chunk of response.body) { |
| 41 | + data += chunk |
| 42 | +} |
| 43 | +console.log(response.statusCode) // 200 |
| 44 | +console.log(JSON.parse(data)) // { hello: 'world' } |
| 45 | + |
| 46 | +server.close() |
| 47 | +proxy.close() |
| 48 | +client.close() |
| 49 | + |
| 50 | +function buildServer () { |
| 51 | + return new Promise((resolve, reject) => { |
| 52 | + const server = createServer() |
| 53 | + server.listen(0, () => resolve(server)) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +function buildProxy () { |
| 58 | + return new Promise((resolve, reject) => { |
| 59 | + const server = proxy(createServer()) |
| 60 | + server.listen(0, () => resolve(server)) |
| 61 | + }) |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Connect with authentication |
| 66 | + |
| 67 | +```js |
| 68 | +import { Client } from 'undici' |
| 69 | +import { createServer } from 'http' |
| 70 | +import proxy = from 'proxy' |
| 71 | + |
| 72 | +const server = await buildServer() |
| 73 | +const proxy = await buildProxy() |
| 74 | + |
| 75 | +const serverUrl = `http://localhost:${server.address().port}` |
| 76 | +const proxyUrl = `http://localhost:${proxy.address().port}` |
| 77 | + |
| 78 | +proxy.authenticate = function (req, fn) { |
| 79 | + fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`) |
| 80 | +} |
| 81 | + |
| 82 | +server.on('request', (req, res) => { |
| 83 | + console.log(req.url) // '/hello?foo=bar' |
| 84 | + res.setHeader('content-type', 'application/json') |
| 85 | + res.end(JSON.stringify({ hello: 'world' })) |
| 86 | +}) |
| 87 | + |
| 88 | +const client = new Client(proxyUrl) |
| 89 | + |
| 90 | +const response = await client.request({ |
| 91 | + method: 'GET', |
| 92 | + path: serverUrl + '/hello?foo=bar', |
| 93 | + headers: { |
| 94 | + 'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}` |
| 95 | + } |
| 96 | +}) |
| 97 | + |
| 98 | +response.body.setEncoding('utf8') |
| 99 | +let data = '' |
| 100 | +for await (const chunk of response.body) { |
| 101 | + data += chunk |
| 102 | +} |
| 103 | +console.log(response.statusCode) // 200 |
| 104 | +console.log(JSON.parse(data)) // { hello: 'world' } |
| 105 | + |
| 106 | +server.close() |
| 107 | +proxy.close() |
| 108 | +client.close() |
| 109 | + |
| 110 | +function buildServer () { |
| 111 | + return new Promise((resolve, reject) => { |
| 112 | + const server = createServer() |
| 113 | + server.listen(0, () => resolve(server)) |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +function buildProxy () { |
| 118 | + return new Promise((resolve, reject) => { |
| 119 | + const server = proxy(createServer()) |
| 120 | + server.listen(0, () => resolve(server)) |
| 121 | + }) |
| 122 | +} |
| 123 | +``` |
0 commit comments