diff options
author | 2022-04-01 19:53:02 -0700 | |
---|---|---|
committer | 2022-04-01 19:53:02 -0700 | |
commit | 08bf4694caaadaffe26828ced639b0b0fc3ca9ec (patch) | |
tree | f356463ceb704da03f8a3c93649ed3d7a189ee86 | |
parent | 60cc51379db07291742b1dd4345971de933b5fba (diff) | |
download | bun-08bf4694caaadaffe26828ced639b0b0fc3ca9ec.tar.gz bun-08bf4694caaadaffe26828ced639b0b0fc3ca9ec.tar.zst bun-08bf4694caaadaffe26828ced639b0b0fc3ca9ec.zip |
http file example
-rw-r--r-- | examples/bun/http-file.ts | 25 | ||||
-rw-r--r-- | examples/bun/http.ts (renamed from examples/bun/htttp.ts) | 0 |
2 files changed, 25 insertions, 0 deletions
diff --git a/examples/bun/http-file.ts b/examples/bun/http-file.ts new file mode 100644 index 000000000..9d0a2362f --- /dev/null +++ b/examples/bun/http-file.ts @@ -0,0 +1,25 @@ +// Start a fast HTTP server from a function +Bun.serve({ + fetch(req) { + const url = new URL(req.url.substring(1), "file://" + import.meta.path); + const path = Bun.resolveSync(url.pathname, import.meta.path); + return new Response(Bun.file(path)); + }, + + // this is called when fetch() throws or rejects + error(err: Error) { + return new Response("uh oh! :(" + String(err.toString()), { status: 500 }); + }, + + // this boolean enables the bun's default error handler + // sometime after the initial release, it will auto reload as well + development: process.env.NODE_ENV !== "production", + // note: this isn't node, but for compatibility bun supports process.env + more stuff in process + + // SSL is enabled if these two are set + // certFile: './cert.pem', + // keyFile: './key.pem', + + port: 8080, // number or string + hostname: "localhost", // defaults to 0.0.0.0 +}); diff --git a/examples/bun/htttp.ts b/examples/bun/http.ts index c528cdd02..c528cdd02 100644 --- a/examples/bun/htttp.ts +++ b/examples/bun/http.ts |