Git Product home page Git Product logo

rou3's Introduction

๐ŸŒณ rou3

npm version npm downloads bundle size codecov

Lightweight and fast router for JavaScript.

Note

Radix3 migrated to Rou3! See #108 for notes and radix3 branch for legacy codebase.

Usage

Install:

# โœจ Auto-detect
npx nypm install rou3

# npm
npm install rou3

# yarn
yarn add rou3

# pnpm
pnpm install rou3

# bun
bun install rou3

Import:

ESM (Node.js, Bun)

import {
  createRouter,
  addRoute,
  findRoute,
  removeRoute,
  findAllRoutes,
} from "rou3";

CommonJS (Legacy Node.js)

const {
  createRouter,
  addRoute,
  findRoute,
  removeRoute,
  findAllRoutes,
} = require("rou3");

CDN (Deno, Bun and Browsers)

import {
  createRouter,
  addRoute,
  findRoute,
  removeRoute,
  findAllRoutes,
} from "https://esm.sh/rou3";

Create a router instance and insert routes:

import { createRouter, addRoute } from "rou3";

const router = createRouter(/* options */);

addRoute(router, "GET", "/path", { payload: "this path" });
addRoute(router, "POST", "/path/:name", { payload: "named route" });
addRoute(router, "GET", "/path/foo/**", { payload: "wildcard route" });
addRoute(router, "GET", "/path/foo/**:name", {
  payload: "named wildcard route",
});

Match route to access matched data:

// Returns [{ payload: 'this path' }]
findRoute(router, "GET", "/path");

// Returns [{ payload: 'named route', params: { name: 'fooval' } }]
findRoute(router, "POST", "/path/fooval");

// Returns [{ payload: 'wildcard route' }]
findRoute(router, "GET", "/path/foo/bar/baz");

// Returns undefined (no route matched for/)
findRoute(router, "GET", "/");

License

Published under the MIT license. Made by @pi0 and community ๐Ÿ’›


๐Ÿค– auto updated with automd

rou3's People

Contributors

pi0 avatar renovate[bot] avatar danielroe avatar autofix-ci[bot] avatar atinux avatar andrewwalsh avatar 20051231 avatar harlan-zw avatar wobsoriano avatar rslabbert avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.