Routing
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 val 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 val.
Hono
Here’s an example with Hono:
import { Hono } from "npm:hono@3";
const app = new Hono();app.get("/", (c) => c.text("Hello world!"));app.get("/yeah", (c) => c.text("Routing!"));export default app.fetch;
Peko
And one with Peko:
import { 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? There's something at /hello")); server.get("/hello", () => new Response("Hello world!")); return server.requestHandler(request);};
nhttp
And nhttp:
import { nhttp } from "npm:nhttp-land@1";
export const nhttpExample = async (request) => { const app = nhttp(); app.get("/", () => { return "Hello, World"; }); app.get("/cat", () => { return { name: "cat" }; }); return app.handleRequest(request);};
itty-router
A super tiny example with itty-router:
import { Router, json } from "npm:itty-router@4";
export const ittyRouterExample = async (request: Request) => { const router = Router(); router.get("/", () => "Hi"); return router.handle(request).then(json);};
feTS
A simple example of using feTS server:
import { createRouter, Response } from "npm:fets";
export const router = createRouter().route({ method: "GET", path: "/greetings", schemas: { responses: { 200: { type: "object", properties: { message: { type: "string", }, }, required: ["message"], additionalProperties: false, }, }, }, handler: () => Response.json({ message: "Hello World!" }),});
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);