Skip to main content

Examples

A collection of code examples demonstrating certain functions of Deno Deploy.

  • Hello World

    CLI
    Deploy
    HTTP

    A HTTP server that responds with a static "Hello World" string to all requests.

    function handler(_req: Request): Response {
      return new Response("Hello, World");
    }
    
  • Respond with JSON

    CLI
    Deploy
    HTTP

    A HTTP server that responds to requests with pretty printed JSON.

    function handler(_req: Request) {
      const data = {
        Hello: "World!",
      };
      const body = JSON.stringify(data, null, 2);
      return new Response(body, {
        headers: { "content-type": "application/json; charset=utf-8" },
      });
    }
    
  • Redirects

    CLI
    Deploy
    HTTP

    A HTTP server that that redirects users to https://example.com

    function handler(_req: Request) {
      return Response.redirect("https://example.com", 307);
    }
    
  • A HTTP server that responds to requests with the client's IP address.

    function handler(_req: Request, connInfo: Deno.ServeHandlerInfo) {
      const addr = connInfo.remoteAddr;
      const ip = addr.hostname;
      return new Response(`Your IP address is <b>${ip}</b>`, {
        headers: { "content-type": "text/html" },
      });
    }
    
  • A HTTP server that serves a HTML form and handles the form submission via a POST request.

    const html = `
    <form method="POST" action="/">
      <input type="text" name="name" placeholder="Your name">
      <button type="submit">Submit</button>
    </form>
    `;
    
    async function handler(req: Request): Promise<Response> {
      switch (req.method) {
        case "GET": {
          return new Response(html, {
            headers: { "content-type": "text/html; charset=utf-8" },
          });
        }
    
        case "POST": {
          const body = await req.formData();
          const name = body.get("name") || "anonymous";
          return new Response(`Hello ${name}!`);
        }
    
        default:
          return new Response("Invalid method", { status: 405 });
      }
    }
    
  • A HTTP server that proxies requests to a different server.

    async function handler(req: Request): Promise<Response> {
      const url = new URL(req.url);
      url.protocol = "https:";
      url.hostname = "example.com";
      url.port = "443";
      return await fetch(url.href, {
        headers: req.headers,
        method: req.method,
        body: req.body,
      });
    }
    
  • A HTTP server that renders a HTML page on the server with JSX (using Preact).

    import { h } from "https://esm.sh/preact@10.5.15";
    import { renderToString } from "https://esm.sh/preact-render-to-string@5.1.19?deps=preact@10.5.15";
    
    function handler(_req: Request): Response {
      const page = (
        <div>
          <h1>Current time</h1>
          <p>{new Date().toLocaleString()}</p>
        </div>
      );
      const html = renderToString(page);
      return new Response(html, {
        headers: { "content-type": "text/html; charset=utf-8" },
      });
    }
    
  • Wildcard Domain

    CLI
    Deploy
    HTTP

    A HTTP server that serves a wildcard domain.

    function handler(req: Request) {
      const url = new URL(req.url);
      if (url.hostname === "a.example.com") {
        return new Response("website 1");
      } else if (url.hostname === "b.example.com") {
        return new Response("website 2");
      }
      return new Response("website infinity");
    }