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
Unique identifier for the route
method
string | string[]
required
HTTP method(s): 'GET', 'POST', 'PUT', 'DELETE', or array like ['GET', 'POST']
Whether this is an admin route
Whether this is an API route
Human-readable name for the route
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
method
string | string[]
required
HTTP method(s)
URL path (will be used as-is for storefront)
Display name for the route
Whether this is an API endpoint
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
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
Use Unique Route IDs
Route IDs must be unique across the entire application. Use descriptive, namespaced IDs like catalogProductList.
Follow Naming Conventions
Use the file naming pattern [METHOD]routeName[status].js for auto-discovery.
Organize by Module
Keep routes in their respective module directories under pages/frontStore/ or pages/admin/.
Specify HTTP Methods Correctly
Use appropriate HTTP methods: GET for retrieval, POST for creation, PUT for updates, DELETE for removal.
Use Access Control
Set access: 'public' for public pages, 'private' for authenticated-only routes.
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 >
);
};