Skip to main content

Overview

EverShop’s Router API provides a centralized system for registering and managing routes for both the admin panel and storefront. Routes are automatically discovered from module directories and can be dynamically registered.

Router Class

The core router singleton manages all application routes. Location: packages/evershop/src/lib/router/Router.js:3

Router Methods

addRoute()

Add or update a route in the router.
import { addRoute } from '@evershop/evershop/src/lib/router/Router';

addRoute({
  id: 'productList',
  method: 'GET',
  path: '/products',
  isAdmin: false,
  isApi: false
});
Location: packages/evershop/src/lib/router/Router.js:43
Parameters
route
object
required

hasRoute()

Check if a route exists by ID.
import { hasRoute } from '@evershop/evershop/src/lib/router/Router';

if (hasRoute('productList')) {
  console.log('Route exists');
}
Location: packages/evershop/src/lib/router/Router.js:47
Signature
function hasRoute(id: string): boolean

getRoutes()

Get all routes sorted by priority.
import { getRoutes } from '@evershop/evershop/src/lib/router/Router';

const routes = getRoutes();
Location: packages/evershop/src/lib/router/Router.js:46
Returns
Array of all routes sorted by their URL path specificity (more specific routes first).

getRoute()

Get a specific route by ID.
import { getRoute } from '@evershop/evershop/src/lib/router/Router';

const route = getRoute('productList');
Location: packages/evershop/src/lib/router/Router.js:49

getFrontStoreRoutes()

Get all storefront routes.
import { getFrontStoreRoutes } from '@evershop/evershop/src/lib/router/Router';

const storeRoutes = getFrontStoreRoutes();
Location: packages/evershop/src/lib/router/Router.js:44

getAdminRoutes()

Get all admin routes.
import { getAdminRoutes } from '@evershop/evershop/src/lib/router/Router';

const adminRoutes = getAdminRoutes();
Location: packages/evershop/src/lib/router/Router.js:45

deleteRoute()

Remove a route by ID.
import { deleteRoute } from '@evershop/evershop/src/lib/router/Router';

deleteRoute('productList');
Location: packages/evershop/src/lib/router/Router.js:48

empty()

Clear all routes from the router.
import { empty } from '@evershop/evershop/src/lib/router/Router';

empty();
Location: packages/evershop/src/lib/router/Router.js:50

Route Registration

registerFrontStoreRoute()

Register a storefront route.
import { registerFrontStoreRoute } from '@evershop/evershop/src/lib/router/registerFrontStoreRoute';

registerFrontStoreRoute(
  'productList',           // id
  'GET',                   // method
  '/products',             // path
  'Product List',          // name
  false,                   // isApi
  '/modules/catalog/pages', // folder
  null,                    // payloadSchema
  'public'                 // access: 'public' or 'private'
);
Location: packages/evershop/src/lib/router/registerFrontStoreRoute.js:11

Signature

function registerFrontStoreRoute(
  id: string,
  method: string | string[],
  path: string,
  name: string,
  isApi?: boolean,
  folder?: string,
  payloadSchema?: object | null,
  access?: 'public' | 'private'
): void

Parameters

id
string
required
Unique route identifier
method
string | string[]
required
HTTP method(s)
path
string
required
URL path (will be used as-is for storefront)
name
string
required
Display name for the route
isApi
boolean
default:"false"
Whether this is an API endpoint
folder
string
default:"''"
Module folder path
payloadSchema
object
default:"null"
JSON schema for request payload validation
access
string
default:"'private'"
Access control: 'public' or 'private'

registerAdminRoute()

Register an admin panel route.
import { registerAdminRoute } from '@evershop/evershop/src/lib/router/registerAdminRoute';

registerAdminRoute(
  'adminProductList',      // id
  'GET',                   // method
  '/products',             // path (will become /admin/products)
  'Product Management',    // name
  false,                   // isApi
  '/modules/catalog/pages' // folder
);
Location: packages/evershop/src/lib/router/registerAdminRoute.js:11

Signature

function registerAdminRoute(
  id: string,
  method: string | string[],
  path: string,
  name: string,
  isApi?: boolean,
  folder?: string
): void

Path Transformation

Admin routes automatically prefix paths with /admin:
registerAdminRoute('dashboard', 'GET', '/', 'Dashboard');
// Actual path: /admin

registerAdminRoute('products', 'GET', '/products', 'Products');
// Actual path: /admin/products
Implementation: packages/evershop/src/lib/router/registerAdminRoute.js:27

Route File Convention

Routes are automatically discovered from module directories:

File Naming Pattern

[method][routeName][status].js
Examples:
  • [GET]products[200].js - GET /products
  • [POST]checkout[200].js - POST /checkout
  • [PUT]product[id][200].js - PUT /product/:id

Directory Structure

modules/
└── catalog/
    └── pages/
        ├── frontStore/
        │   ├── [GET]products[200].js
        │   └── [GET]product[slug][200].js
        └── admin/
            ├── [GET]productGrid[200].js
            └── [POST]productSave[200].js

Route Parameters

Dynamic route parameters use bracket notation:
// Route file: [GET]product[id][200].js
registerFrontStoreRoute(
  'productDetail',
  'GET',
  '/product/:id',
  'Product Detail'
);

// Accessing parameter in handler:
export default async function handler(request, response) {
  const { id } = request.params;
  // ...
}

API Routes

API routes return JSON instead of rendering pages:
registerFrontStoreRoute(
  'apiProductSearch',
  'POST',
  '/api/products/search',
  'Product Search API',
  true  // isApi = true
);

Best Practices

1

Use Unique Route IDs

Route IDs must be unique across the entire application. Use descriptive, namespaced IDs like catalogProductList.
2

Follow Naming Conventions

Use the file naming pattern [METHOD]routeName[status].js for auto-discovery.
3

Organize by Module

Keep routes in their respective module directories under pages/frontStore/ or pages/admin/.
4

Specify HTTP Methods Correctly

Use appropriate HTTP methods: GET for retrieval, POST for creation, PUT for updates, DELETE for removal.
5

Use Access Control

Set access: 'public' for public pages, 'private' for authenticated-only routes.
6

Document API Routes

Mark API endpoints with isApi: true for proper handling and documentation.

Example: Complete Route Setup

// modules/catalog/pages/frontStore/[GET]products[200].js
import { registerFrontStoreRoute } from '@evershop/evershop/src/lib/router/registerFrontStoreRoute';

// Register route
registerFrontStoreRoute(
  'catalogProductList',
  'GET',
  '/products',
  'Product Listing',
  false,  // Not an API route
  __dirname,
  null,
  'public'  // Public access
);

// Route handler
export default async function productList(request, response) {
  // Your route logic
  const products = await getProducts();
  
  return {
    products
  };
}

// Page component
export const ProductListPage = () => {
  return (
    <div>
      <h1>Products</h1>
      {/* Product listing UI */}
    </div>
  );
};

Build docs developers (and LLMs) love