summaryrefslogtreecommitdiffstats
path: root/examples/proxy/index.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
commit0b6210cd37b68b94252cb798598b12974a20e1c1 (patch)
treee371686554a877842d95aa94f100bee552ff2a8e /examples/proxy/index.js
parentInitial commit. (diff)
downloadnode-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.tar.xz
node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.zip
Adding upstream version 5.28.2+dfsg1+~cs23.11.12.3.upstream/5.28.2+dfsg1+_cs23.11.12.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/proxy/index.js')
-rw-r--r--examples/proxy/index.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/proxy/index.js b/examples/proxy/index.js
new file mode 100644
index 0000000..5f35049
--- /dev/null
+++ b/examples/proxy/index.js
@@ -0,0 +1,49 @@
+const { Pool, Client } = require('../../')
+const http = require('http')
+const proxy = require('./proxy')
+
+const pool = new Pool('http://localhost:4001', {
+ connections: 256,
+ pipelining: 1
+})
+
+async function run () {
+ await Promise.all([
+ new Promise(resolve => {
+ // Proxy
+ http.createServer((req, res) => {
+ proxy({ req, res, proxyName: 'example' }, pool).catch(err => {
+ if (res.headersSent) {
+ res.destroy(err)
+ } else {
+ for (const name of res.getHeaderNames()) {
+ res.removeHeader(name)
+ }
+ res.statusCode = err.statusCode || 500
+ res.end()
+ }
+ })
+ }).listen(4000, resolve)
+ }),
+ new Promise(resolve => {
+ // Upstream
+ http.createServer((req, res) => {
+ res.end('hello world')
+ }).listen(4001, resolve)
+ })
+ ])
+
+ const client = new Client('http://localhost:4000')
+ const { body } = await client.request({
+ method: 'GET',
+ path: '/'
+ })
+
+ for await (const chunk of body) {
+ console.log(String(chunk))
+ }
+}
+
+run()
+
+// TODO: Add websocket example.