Start an HTTP server on a unix domain socket with Bun.serve()
Bun.serve() now supports unix domain sockets, which let you point a socket to a file on your filesystem instead of a network host/port. This is useful when you want to run a server that’s only accessible from the same machine, sometimes containers or proxies.
const server = Bun.serve({
unix: "/tmp/my-socket.sock", // <-- new option
fetch(req){
console.log(req.url);
return new Response("Hello world!");
}
});
console.log(`Listening on unix:///tmp/my-socket.sock!`);
To start the server, run bun ./server.ts
.
Then, you can use curl
to make a request to the
socket.
curl --unix-socket /tmp/my-socket.sock http://localhost/my-path