Simple HTTP web server

Concepts

  • Use the std library http module to run your own web server.

Overview

With just a few lines of code you can run your own http web server with control over the response status, request headers and more.

Sample web server

In this example, the user-agent of the client is returned to the client:

  1. /**
  2. * webserver.ts
  3. */
  4. import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
  5. const server = serve({ hostname: "0.0.0.0", port: 8080 });
  6. console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);
  7. for await (const request of server) {
  8. let bodyContent = "Your user-agent is:nn";
  9. bodyContent += request.headers.get("user-agent") || "Unknown";
  10. request.respond({ status: 200, body: bodyContent });
  11. }

Run this with:

  1. deno run --allow-net webserver.ts