diff options
author | 2022-03-26 03:49:44 -0700 | |
---|---|---|
committer | 2022-03-26 03:49:44 -0700 | |
commit | 75138577cd5e471dcc8cad4d7eb14cd56f918b8a (patch) | |
tree | e518f764a7991c7bc9bfd8ed15a62aced57cd61a | |
parent | 889ce659747bbb380f98712d6830d98da75fd97a (diff) | |
download | bun-75138577cd5e471dcc8cad4d7eb14cd56f918b8a.tar.gz bun-75138577cd5e471dcc8cad4d7eb14cd56f918b8a.tar.zst bun-75138577cd5e471dcc8cad4d7eb14cd56f918b8a.zip |
Create htttp.ts
Diffstat (limited to '')
-rw-r--r-- | examples/bun/htttp.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/bun/htttp.ts b/examples/bun/htttp.ts new file mode 100644 index 000000000..c528cdd02 --- /dev/null +++ b/examples/bun/htttp.ts @@ -0,0 +1,35 @@ +// Start a fast HTTP server from a function +Bun.serve({ + fetch(req) { + return new Response("Hello World!"); + }, + + // this is called when fetch() throws or rejects + error(err: Error) { + return new Response("uh oh! :(" + 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 +}); + +// Start a fast HTTP server from the main file's export +export default { + fetch(req) { + return new Response( + `This is another way to start a server! + if the main file export default's an object + with 'fetch'. Bun automatically calls Bun.serve` + ); + }, + // so autocomplete & type checking works +} as Bun.Serve; |