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 " ;
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 ) => {
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 ) => {
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 ( {
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 inferred
const response = await client[ " /greetings " ] . get ();
const greetings = await response . json ();