aboutsummaryrefslogtreecommitdiff
path: root/examples/html-rewriter.ts
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-07 19:29:01 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-07 19:29:01 -0700
commit91fb170c4a9c8885436791e5be77924de2e08b0c (patch)
tree6e8a0e2eed51d9f68f08884e21d2b3f52907fc0e /examples/html-rewriter.ts
parent0878659513270511f64a039ce7ddaedf296fc68a (diff)
downloadbun-91fb170c4a9c8885436791e5be77924de2e08b0c.tar.gz
bun-91fb170c4a9c8885436791e5be77924de2e08b0c.tar.zst
bun-91fb170c4a9c8885436791e5be77924de2e08b0c.zip
Update html-rewriter.ts
Diffstat (limited to 'examples/html-rewriter.ts')
-rw-r--r--examples/html-rewriter.ts18
1 files changed, 9 insertions, 9 deletions
diff --git a/examples/html-rewriter.ts b/examples/html-rewriter.ts
index 2b370c5ed..a2a9d03fa 100644
--- a/examples/html-rewriter.ts
+++ b/examples/html-rewriter.ts
@@ -1,7 +1,10 @@
// Start a fast HTTP server from a function
Bun.serve({
async fetch(req) {
- if (!(req.url.startsWith("/https://") || req.url.startsWith("/http://"))) {
+ const { pathname } = new URL(req.url);
+ if (
+ !(pathname.startsWith("/https://") || pathname.startsWith("/http://"))
+ ) {
return new Response(
"Enter a path that starts with https:// or http://\n",
{
@@ -10,12 +13,10 @@ Bun.serve({
);
}
- const url = new URL(req.url.substring(1));
- const response = await fetch(url.toString(), req.clone());
-
- if (!response.headers.get("Content-Type").includes("html")) {
- return response;
- }
+ const response = await fetch(
+ req.url.substring("http://localhost:3000/".length),
+ req.clone()
+ );
return new HTMLRewriter()
.on("a[href]", {
@@ -42,6 +43,5 @@ Bun.serve({
// certFile: './cert.pem',
// keyFile: './key.pem',
- port: 8080, // number or string
- hostname: "localhost", // defaults to 0.0.0.0
+ port: 3000, // number or string
});