npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serve-router

v1.1.0

Published

tiny router library that routes for your web standard http server

Downloads

201

Readme

serve-router

npm version bundle paka.dev TSDocs JSDocs License

A tiny router library that routes for your web standard HTTP server.

Build for Deno's Deno.serve() api, which is also compatible with some serverless platforms. 
e.g. Bun.serve() Cloudflare Workers val.town Web API  
see: https://deno.com/manual/runtime/http_server_apis  
Node.js is also supported with built-in polyfill.

features:

  • Works in any runtime - Node.js, Bun, Deno, Cloudflare Workers, even the web browser!
  • Easy to use with a familiar API
  • Integrate with the existing ecosystem
  • Fully typed
  • Zero dependency

[!NOTE]
For those who are building large web application, Hono may be preferred.

Usage

We use the URL Pattern API for matching.   It's based on the path-to-regexp library, the one that express depends.   For details, see the standard.

For environments that do not support URLPattern(), just import urlpattern-polyfill.

Requirement: support Web Standard API: Request Response.   For Node.js < 18, this means polyfill is required.

// use Deno
import ServeRouter from "https://esm.sh/serve-router@latest"
const { serve } = Deno // Deno >= 1.15 support URLPattern()

// use Node.js >= 18 or Bun
import "urlpattern-polyfill" // Node.js and Bun do not support URLPattern() currently
import ServeRouter from "serve-router"
import { serve } from "serve-router/node" // polyfill for Deno.serve(), not required for bun

// init application (context is optional)
const app = ServeRouter()

app.all("/", () => new Response("Hello, world!"))

app.get("/headers", (request: Request) =>
    Response.json(Object.fromEntries(request.headers.entries()))
)

app.get("/user/:name", (_req, { params }) => {
    // (auto-inferred types)
    // params: { name: string }
    return new Response(`Hello, ${params.name}`)
    // (To disable params infer)
    // app.get("/user/:name" as string, ...)
})

app.post("/post", async (req) => {
    const json = await req.json()
    return Response.json(json)
})

app.route("/api") // for /api
    .get("", () => new Response("api")) // for /api/one
    .get("/one", () => new Response("one")) // for /api/two
    .get("/two", () => new Response("two"))

app.use("/**", async (_req, { next }) => {
    const start = Date.now()
    const resp = await next()
    const ms = Date.now() - start

    return new Response(resp.body, {
        status: resp.status,
        headers: { ...Object.fromEntries(resp.headers.entries()), "X-Response-Time": `${ms}ms` },
    })
    // OR:
    // import { transformResponse } from "serve-router/utils"
    // return transformResponse(resp, { headers: { "X-Response-Time": `${ms}ms` } })
})

// Pitfall:
// if you want to catch all when other route does not match, use app.all()
app.all("/*", (_req) => {
    return new Response("Catch!", { status: 404 })
})
// be aware that code below will override all matched route, which may not be what you want
app.use("/*", async (_req, { next }) => {
    await next() // run other middlewares
    return new Response("Oops!", { status: 404 }) // returns `Oops!` for any request
})

// for Deno or Node.js
serve(app.fetch)
// for Bun
Bun.serve({ fetch: app.fetch })

If you only want to run it in Node.js, you can convert it into a Node.js http middleware like this:

import "urlpattern-polyfill"
import { d2n } from "serve-router/node"
const app = ServeRouter()
app.get("/", () => new Response("Hello, world!"))
http.createServer(d2n(app.fetch)).listen(8080)

This library supports CommonJS environment.

require("urlpattern-polyfill")
const { d2n, serve } = require("serve-router/node")
const { ServeRouter } = require("serve-router")
const http = require("http")
const app = ServeRouter()
app.get("/", () => new Response("Hello, world!"))

serve({ handler: app.fetch, port: 8080 })
// OR:
// http.createServer(d2n(app.fetch)).listen(8080)

For Cloudflare Workers, Bun and Deno

const app = ServeRouter()
app.get("/", () => new Response("Hello, world!"))
export default {
    fetch: app.fetch,
}

Advanced

// this example use Deno
import ServeRouter from "npm:serve-router"
import { serveDir } from "jsr:@std/http/file-server"
import { setCookie, getCookies, type Cookie } from "jsr:@std/http/cookie"

const app = ServeRouter({
    context: { beginTime: Date.now() },
    onError(err) {
        console.log(err)
    },
})
// OR: const app = ServeRouter<{ beginTime: number }>()

app.all("/static/(.*)", (req) =>
    serveDir(req, {
        urlRoot: "static",
        fsRoot: "public",
    })
)

app.get("/", async (req) => {
    const cookies: Record<string, string> = getCookies(req.headers)
    const visitTimes = Number(cookies.visitTimes) || 0

    const headers = new Headers()
    const cookie: Cookie = { name: "visitTimes", value: String(visitTimes + 1) }
    setCookie(headers, cookie)

    return new Response(`Visit ${visitTimes} ${visitTimes > 0 ? "times" : "time"}!`, { headers })
})

app.use<{ beginTime: number }>("/(.*)", async (req, ctx) => {
    ctx.beginTime = Date.now()
    console.log("[prehandle]  ", req.method, req.url)
    const resp = await ctx.next()
    const endTime = Date.now()
    console.log("[posthandle] ", `Returned ${resp.status} in ${endTime - ctx.beginTime}ms`)
    return resp
})

app.all<{ beginTime: number }>("/(.*)", async (_req, ctx) => {
    return new Response(`Request begins at ${new Date(ctx.beginTime)}`)
})

Deno.serve(app.fetch)

Yes! You can run it in the browser!

<script type="module">
    import ServeRouter from "https://esm.sh/serve-router"

    const app = ServeRouter()
    app.get("/:name", (req, ctx) => new Response(`Hello, ${ctx.params.name}!`))

    const response = await app.fetch(new Request("https://any.domain/alice"))
    console.log(await response.text()) // => "Hello, alice!"
</script>

Build

git clone https://github.com/YieldRay/serve-router.git
cd serve-router
pnpm install
pnpm run build