|
1 | | -import { listen } from "listhen"; |
2 | | -import { createProxyServer } from "../src"; |
| 1 | +import http from "node:http"; |
| 2 | +import { createProxyServer } from "../src/index.ts"; |
3 | 3 |
|
4 | 4 | async function main() { |
5 | | - const main = await listen( |
6 | | - (req, res) => { |
7 | | - res.end( |
8 | | - JSON.stringify({ |
9 | | - method: req.method, |
10 | | - path: req.url, |
11 | | - headers: req.headers, |
12 | | - }), |
13 | | - ); |
14 | | - }, |
15 | | - { port: 3000, name: "main" }, |
16 | | - ); |
| 5 | + const main = http.createServer((req, res) => { |
| 6 | + res.end( |
| 7 | + JSON.stringify({ |
| 8 | + method: req.method, |
| 9 | + path: req.url, |
| 10 | + headers: req.headers, |
| 11 | + }), |
| 12 | + ); |
| 13 | + }); |
| 14 | + await new Promise<void>((resolve) => { |
| 15 | + main.listen(3000, "127.0.0.1", resolve); |
| 16 | + }); |
17 | 17 |
|
18 | 18 | const httpProxy = createProxyServer(); |
19 | 19 |
|
20 | | - await listen( |
21 | | - async (req, res) => { |
22 | | - try { |
23 | | - await httpProxy.web(req, res, { |
24 | | - target: main.url, |
25 | | - }); |
26 | | - } catch (error) { |
27 | | - console.error(error); |
28 | | - res.statusCode = 500; |
29 | | - res.end("Proxy error: " + error.toString()); |
30 | | - } |
31 | | - }, |
32 | | - { port: 3001, name: "proxy" }, |
33 | | - ); |
| 20 | + const proxy = http.createServer(async (req, res) => { |
| 21 | + try { |
| 22 | + await httpProxy.web(req, res, { |
| 23 | + target: "http://127.0.0.1:3000", |
| 24 | + }); |
| 25 | + } catch (error) { |
| 26 | + console.error(error); |
| 27 | + res.statusCode = 500; |
| 28 | + res.end("Proxy error: " + (error as Error).toString()); |
| 29 | + } |
| 30 | + }); |
| 31 | + await new Promise<void>((resolve) => { |
| 32 | + proxy.listen(3001, "127.0.0.1", resolve); |
| 33 | + }); |
| 34 | + |
| 35 | + console.log("main: http://127.0.0.1:3000"); |
| 36 | + console.log("proxy: http://127.0.0.1:3001"); |
34 | 37 | } |
35 | 38 |
|
36 | 39 | // eslint-disable-next-line unicorn/prefer-top-level-await |
|
0 commit comments