Skip to content

Instantly share code, notes, and snippets.

@dimasjt
Created December 8, 2023 14:42
Show Gist options
  • Save dimasjt/939fb313d2530d3cad5339f179149f4c to your computer and use it in GitHub Desktop.
Save dimasjt/939fb313d2530d3cad5339f179149f4c to your computer and use it in GitHub Desktop.
https SSL for Bun and NextJS. using proxy

PORT=3001 bun run dev & PORT=3000 bun run proxy.ts

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