Route

Overview

npm i @webroute/route

Webroute makes building fully-fledged standalone HTTP routes easy. The end result can be run anywhere web standard Request and Response are supported, including without any router.

Conceptually, route moves middleware and I/O validation out of the router and into the route itself. This enables powerful composition and each route becomes incredibly portable.


Introduction

The utility of this approach becomes clear when used with nextjs or other filesystem routers, for example:

export const  = ()
  // Specify middleware, returning data will update the `state` field later
  .(() => {
    return { : "123" };
  })
 
  // Specify any input and output validators...
  .(.({ : .() }))
 
  // Handle the request...
  .(async (, { ,  }) => {
    const {  } = ;
    const {  } = await ("body"); // Lazily validate body
 
    return { : true }; // Sends as JSON
    // or: return Response.json(...)
  });
 
// Just a regular request handler
const  = await (new ("https://google.com"));

And you could use this exact same route handler with Hono too.

app.get("/post", (c) => GET(c.req.raw));

By packaging the validation and middleware (and more) with the route itself, it becomes incredibly portable, and you'll find your applications actually become simpler too. Learn more about what webroute includes in Features or get up and running via the Quick Start.

On this page