Router
A high-performance basic router works everywhere.
Server: See zhmushan/abc
React: Coming soon…
Browser:
<body>
<button id="change_path">Change Path</button>
<button id="home">Home</button>
<script type="module">
import { Node } from "https://deno.land/x/router@v1.0.0-rc1/mod.js";
const root = new Node();
root.add("/:random_string", (c) => {
console.log(c.get("random_string"));
});
change_path.onclick = () => {
const path = `/${randomStr()}`;
const [func, params] = root.find(path);
if (func) {
func(params);
history.replaceState(undefined, "", path);
}
};
home.onclick = () => {
history.replaceState(undefined, "", "/");
};
function randomStr() {
return Math.random().toString(32).split(".")[1];
}
</script>
</body>