PORT=3001 bun run dev & PORT=3000 bun run proxy.ts
Created
December 8, 2023 14:42
-
-
Save dimasjt/939fb313d2530d3cad5339f179149f4c to your computer and use it in GitHub Desktop.
https SSL for Bun and NextJS. using proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from "fs"; | |
const port = process.env.PORT || 3001; | |
Bun.serve({ | |
port, | |
async fetch(req) { | |
try { | |
const parsedUrl = new URL(req.url); | |
parsedUrl.port = "3001"; | |
parsedUrl.protocol = "http"; | |
console.log("url", parsedUrl.toString()); | |
const response = await fetch(parsedUrl.toString(), { | |
method: req.method, | |
headers: req.headers, | |
}); | |
for (const [key, value] of response.headers.entries()) { | |
if (["content-encoding"].includes(key)) { | |
response.headers.delete(key); | |
} | |
} | |
return response; | |
} catch (err) { | |
console.error(err); | |
return new Response(err.message, { status: 500 }); | |
} | |
}, | |
tls: { | |
key: fs.readFileSync("./server/certs/localhost-key.pem"), | |
cert: fs.readFileSync("./server/certs/localhost.pem"), | |
}, | |
}); | |
console.log(`Server running at https://localhost:${port}/`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment