breeze-router
v1.1.0
Published
A lightweight, zero-dependency client-side router for single page applications (SPAs).
Downloads
352
Maintainers
Readme
Breeze Router
A lightweight, zero-dependency client-side router for single page applications (SPAs).
Installation
To use this router in your project, install the router using npm:
1. Through NPM
npm install breeze-router2. Through CDN link
<script type="module">
import BreezeRouter from
"https://unpkg.com/[email protected]/dist/BreezeRouter.min.js";
</script>Usage
To use the router in your application, you need to import BreezeRouter and define routes and handlers using the Router class:
// NPM.
import BeezeRouter from "breeze-router";
// CDN.
import BreezeRouter from "https://unpkg.com/[email protected]/dist/BreezeRouter.min.js";
// Create a new `BreezeRouter` instance.
const ROUTER = new BreezeRouter();
// Define routes using the `add()` method.
ROUTER.add("/", async () => {
// Handle the root route
});
ROUTER.add("/about", async ({ route, params }) => {
// Handle the about route.
// route.path equals to current route with a trailing splash, e.g.: /about/
});
ROUTER.add("/users/:userId", async ({ route, params }) => {
// Handle the users route with a dynamic parameter :userId
const userId = params.userId;
});
// You can define a type for the placeholder param to
// avoid conflicts with other similar route like "/users/history".
ROUTER.add("/users/:userId<number>", async ({ route, params }) => {
// Handle the users route with a dynamic parameter :userId
const userId = params.userId;
});
ROUTER.add("/users/:username/posts/:postId", async ({ route, params }) => {
// Handle the posts route with a dynamic parameter :username and :userId
const { username, postId } = params;
});
// Call the `start` method to start the router.
ROUTER.start();
// You can register the callbacks to run when the router is ready.
// This is a good time to do some initial work like injecting some
// global headers or footers into the page.
ROUTER.ready(() => {
console.log("ROUTER is ready now.");
});License
This project is licensed under the MIT License - see the LICENSE file for details.
