Skip to content

ArefShojaei/Router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

73 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Router - Lightweight JavaScript SPA Router

A lightweight and powerful client-side routing library for building Single Page Applications (SPA) with pure JavaScript.

Define routes, manage navigation, handle dynamic parameters, apply middleware, and create seamless page transitions without refreshing the browser.

Router SPA Banner

✨ Features

  • πŸš€ Client-side SPA routing
  • πŸ”— Browser History API support
  • πŸ“„ Single and grouped routes
  • πŸ”₯ Dynamic route parameters
  • πŸ›‘ Route middleware system
  • πŸ”€ Route redirection
  • 🏷 Dynamic page titles
  • ⚑ No page refresh navigation
  • πŸͺΆ Lightweight and dependency-free
  • 🧩 Simple and expressive API

πŸ“¦ Installation

Using NPM

npm install @aref-shojaei/router

Using Yarn

yarn add @aref-shojaei/router

πŸš€ Quick Start

Create your app.js file:

import { Router, Route } from "@aref-shojaei/router";

// Configure browser environment
Router.configure({
    window,
    document
});

// Create routes
Route.add("/", () => "Home Page");

Route.group("/auth", () => {
    Route.add("/login", () => "Login Page");
    Route.add("/register", () => "Register Page");
});

// Dynamic routes
Route.add("/users/{id}", ({ params: { id } }) => {
    return `User #${id}`;
});

// Start the router
Router.run();

🌐 HTML Integration

The router automatically intercepts internal links and updates the page without refreshing the browser.

<body>
    <nav>
        <a href="/">Home</a>
        <a href="/auth/login">Login</a>
        <a href="/auth/register">Register</a>
        <a href="/users/10">User Profile</a>
    </nav>

    <div id="root"></div>

    <script src="app.js"></script>
</body>

πŸ— Router Workflow

User Click
     |
     |
 Browser Event
     |
     |
 Router Engine
     |
     |
 Route Matching
     |
     |
 Middleware
     |
     |
 Component Rendering
     |
     |
 DOM Update

πŸ“š Routing Guide

Basic Routes

Create simple routes:

Route.add("/", () => "Welcome Page");

Route.add("/about", () => "About Page");

Route Groups

Organize related routes together.

Route.group("/auth", () => {
    Route.add("/login", () => "Login Page");
    Route.add("/register", () => "Register Page");
});

Generated routes:

/auth/login
/auth/register

Dynamic Routes

Capture values directly from the URL.

Route.add("/users/{id}", ({ params: { id } }) => {
    return `User ID: ${id}`;
});

Multiple parameters:

Route.add(
    "/courses/{category}/{name}",
    ({ params: { category, name } }) => {
        return `${category} / ${name}`;
    }
);

πŸ›‘ Middleware

Middleware allows you to execute custom logic before rendering a route.

Common use cases:

  • Authentication checks
  • Logging
  • Analytics
  • Permissions
  • Data preparation

Example:

const logger = () => {
    console.log("[LOG] Route visited");
};

Route.add(
    "/dashboard",
    () => "Dashboard"
).middleware([logger]);

Group middleware:

Route.group("/admin", () => {
    Route.add("/users", () => "Users");
    Route.add("/settings", () => "Settings");
}).middleware([logger]);

πŸ”€ Route Redirection

Redirect users to another route.

Route.add("/login", () => "Login Page");

Route.add("/dashboard", () => {
    return Route.redirect("/login");
});

🏷 Page Titles

Change the browser tab title dynamically.

Route.add("/", () => "Home")
    .title("My Awesome SPA");

If no title is specified, the router uses the default document.title.


πŸ“‚ Example Project Structure

my-spa/
|
β”œβ”€β”€ index.html
|
β”œβ”€β”€ app.js
|
└── node_modules/

πŸ’‘ Use Cases

Router can be used for:

  • Single Page Applications (SPA)
  • Dashboard interfaces
  • Admin panels
  • Personal websites
  • Progressive Web Applications
  • Educational projects to learn routing concepts

πŸ”₯ Why Router?

Traditional websites require a full page reload for every navigation.

With Router:

Traditional Website

Click Link
    |
HTTP Request
    |
Server Response
    |
Reload Entire Page


SPA Router

Click Link
    |
JavaScript Router
    |
Change Route
    |
Update DOM

This results in a faster and smoother user experience.


🧠 Concepts You Will Learn

By studying this project, you can understand:

  • Browser History API
  • URL parsing
  • Route matching algorithms
  • Dynamic parameters
  • Middleware patterns
  • Event handling
  • Client-side rendering
  • SPA architecture

πŸ‘¨β€πŸ’» Author

Aref Shojaei


⭐ Show Your Support

If this project helps you understand SPA architecture and client-side routing, consider giving it a Star ⭐ on GitHub.

Your support helps the project grow.

Packages

 
 
 

Contributors