Intermediate
clideploy
HTTP Server: Hello World
An example of a HTTP server that serves a "Hello World" message.
Import the http server from std/http.
!--frsh-copybutton:1-->
import { serve } from "https://deno.land/std@0.175.0/http/server.ts";
HTTP servers need a handler function. This function is called for every request that comes in. It must return a `Response`. The handler function can be asynchronous (it may return a `Promise`).
!--frsh-copybutton:2-->
function handler(_req: Request): Response {
return new Response("Hello, World!");
}
To start the server on the default port, call `serve` with the handler.
!--frsh-copybutton:3-->
console.log("Listening on http://localhost:8000");
serve(handler);
Run this example locally using the Deno CLI:
deno run --allow-net https://byexample-wwv03xf36j0g.deno.dev/http-server.ts
Additional resources: