From 4be6406069a98d92fbc7ad9f5c12f9173e672738 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Wed, 24 Apr 2024 14:37:47 -0400 Subject: [PATCH] fix: add basic deno serve section (#442) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bartek IwaƄczuk --- runtime/manual/tools/index.md | 1 + runtime/manual/tools/serve.md | 32 ++++++++++++++++++++++++++++++++ sidebars/runtime.js | 5 +++++ 3 files changed, 38 insertions(+) create mode 100644 runtime/manual/tools/serve.md diff --git a/runtime/manual/tools/index.md b/runtime/manual/tools/index.md index 5a206d32e..14a9922f8 100644 --- a/runtime/manual/tools/index.md +++ b/runtime/manual/tools/index.md @@ -16,6 +16,7 @@ Deno features before they are finalized. - [formatter (`deno fmt`)](./formatter.md) - [linter (`deno lint`)](./linter.md) - [repl (`deno repl`)](./repl.md) +- [server (`deno serve`)](./serve.md) - [task runner (`deno task`)](./task_runner.md) - [test runner (`deno test`)](../basics/testing/index.md) - [vendoring dependencies (`deno vendor`)](./vendor.md) diff --git a/runtime/manual/tools/serve.md b/runtime/manual/tools/serve.md new file mode 100644 index 000000000..9b80518f3 --- /dev/null +++ b/runtime/manual/tools/serve.md @@ -0,0 +1,32 @@ +# `deno serve`, declarative way to write servers + +In addition to `deno run`, Deno offers a `deno serve` command-line option that +automatically configures servers based on the exports of your main module. + +Here's an example of how you can create a simple HTTP server using the `serve` +subcommand: + +```typescript +export default { + async fetch(request) { + return new Response("Hello world!"); + }, +}; +``` + +In this example, the `fetch` function is used to handle incoming HTTP requests. + +The logic inside the `fetch` function can be customized to handle different +types of requests and serve content accordingly: + +```typescript +export default { + async fetch(request) { + if (request.url.startsWith("/json")) { + return Response.json({ hello: "world" }); + } + + return new Response("Hello world!"); + }, +}; +``` diff --git a/sidebars/runtime.js b/sidebars/runtime.js index af3c6f07c..b5ab0fbd2 100644 --- a/sidebars/runtime.js +++ b/sidebars/runtime.js @@ -180,6 +180,11 @@ const sidebars = { label: "deno run", id: "manual/tools/run", }, + { + type: "doc", + label: "deno serve", + id: "manual/tools/serve", + }, { type: "doc", label: "deno task",