runtime/reference/cli/serve.md
Here's an example of how you can create a simple HTTP server with declarative fetch:
export default {
async fetch(_req) {
return new Response("Hello world!");
},
} satisfies Deno.ServeDefaultExport;
The satisfies Deno.ServeDefaultExport type assertion ensures that your
exported object conforms to the expected interface for Deno's HTTP server. This
provides type safety and better editor autocomplete while allowing you to
maintain the inferred types of your implementation.
You can then run the server using the deno serve command:
deno serve server.ts
The logic inside the fetch function can be customized to handle different
types of requests and serve content accordingly:
export default {
async fetch(request) {
if (request.url.endsWith("/json")) {
return Response.json({ hello: "world" });
}
return new Response("Hello world!");
},
} satisfies Deno.ServeDefaultExport;