One of the coolest things about the Request/Response API is that it works with modern web frameworks, so you can use routing and their helper methods! When an
HTTP file is deployed, it is available at a subdomain like
handle-valname.web.val.run, and requests to any subdirectory
or path will be routed to the file.
We typically recommend Hono:
View and run this example on Val Town
import { Hono } from "npm:hono@3";
const app = new Hono();app.get("/", (c) => c.text("Hello from Hono!"));app.get("/yeah", (c) => c.text("Routing!"));export default app.fetch;And one with Peko:
View and run this example on Val Town
import * as Peko from "https://deno.land/x/peko@2.0.0/mod.ts";
export const pekoExample = async (request) => { const server = new Peko.Router(); server.get("/", () => new Response("Yes? Peko is also serving something at /hello")); server.get("/hello", () => new Response("Hello world!")); return server.requestHandler(request);};And nhttp:
View and run this example on Val Town
import { nhttp } from "npm:nhttp-land@1";
export const nhttpExample = async (request) => { const app = nhttp(); app.get("/", () => { return "Hello from nhttp"; }); app.get("/cat", () => { return { name: "cat" }; }); return app.handleRequest(request);};itty-router
Section titled “itty-router”A super tiny example with itty-router:
View and run this example on Val Town
import { json, Router } from "npm:itty-router@4";
export const ittyRouterExample = async (request: Request) => { const router = Router(); router.get("/", () => "Hi from itty-router!"); return router.handle(request).then(json);};A simple example of using feTS server:
View and run this example on Val Town
import { createRouter, Response } from "npm:fets";
export const router = createRouter().route({ method: "GET", path: "/", schemas: { responses: { 200: { type: "object", properties: { message: { type: "string", }, }, required: ["message"], additionalProperties: false, }, }, }, handler: () => Response.json({ message: "Hello from fets!" }),});
export default router.fetch;Notice, that it exports the router, which allows to use the feTS client to make routes type safe:
import { type router } from "https://esm.town/v/user/fetsServer";import { createClient } from "npm:fets";
const client = createClient<typeof router>({ endpoint: "https://user-fetsServer.web.val.run",});
// The `response` and `greetings` have proper types automatically inferredconst response = await client["/greetings"].get();const greetings = await response.json();console.log(greetings);