Examples
A collection of code examples demonstrating certain functions of Deno Deploy.
Hello World
CLIDeployHTTPA HTTP server that responds with a static "Hello World" string to all requests.
import { serve } from "https://deno.land/std@0.140.0/http/server.ts"; function handler(_req: Request): Response { return new Response("Hello, World"); } serve(handler);
Respond with JSON
CLIDeployHTTPA HTTP server that responds to requests with pretty printed JSON.
import { serve } from "https://deno.land/std@0.140.0/http/server.ts"; 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" }, }); } serve(handler);
Redirects
CLIDeployHTTPA HTTP server that that redirects users to https://example.com
import { serve } from "https://deno.land/std@0.140.0/http/server.ts"; function handler(_req: Request) { return Response.redirect("https://example.com", 307); } serve(handler);
Get client IP address
CLIDeployHTTPA 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" }, }); } Deno.serve(handler);
Handling <form> submissions
CLIDeployHTTPA HTTP server that serves a HTML form and handles the form submission via a POST request.
import { serve } from "https://deno.land/std@0.140.0/http/server.ts"; 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 }); } } serve(handler);
Proxying to other servers
CLIDeployHTTPA HTTP server that proxies requests to a different server.
import { serve } from "https://deno.land/std@0.140.0/http/server.ts"; 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, }); } serve(handler);
Server side rendering with JSX
CLIDeployHTTPA HTTP server that renders a HTML page on the server with JSX (using Preact).
/** @jsx h */ import { serve } from "https://deno.land/std@0.140.0/http/server.ts"; 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" }, }); } serve(handler);
Wildcard Domain
CLIDeployHTTPA HTTP server that serves a wildcard domain.
import { serve } from "https://deno.land/std@0.155.0/http/server.ts"; 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"); } serve(handler);