Skip to content

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!

Hono

Here’s an example with Hono:

Hono exampleRun in Val Town ↗
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:

Peko exampleRun in Val Town ↗
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:

NHttp exampleRun in Val Town ↗
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:

Itty exampleRun in Val Town ↗
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:

@user/fetsServerRun in Val Town ↗
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:

@user/fetsClientRun in Val Town ↗
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 inferred
const response = await client["/greetings"].get();
const greetings = await response.json();
console.log(greetings);