Skip to main content

Routing

EverShop uses a file-based routing system with route.json configuration files. These files define how API endpoints are accessed and control the middleware execution order.

route.json Files

Every API endpoint in EverShop requires a route.json file that specifies:
  • HTTP methods allowed
  • URL path
  • Access control (public/private)

Basic Structure

route.json

Configuration Properties

methods

HTTP methods that the endpoint accepts:
Only the methods listed in methods will be accepted. Other methods will return a 405 Method Not Allowed error.

path

The URL path where the endpoint is accessible:
Use Express-style path parameters with :paramName for dynamic routes.

access

Controls authentication requirements:

Middleware Execution Order

EverShop uses filename conventions to control middleware execution order. Files are executed alphabetically, and you can use brackets [name] to specify dependencies.

Naming Convention

Format: [prerequisite]handlerName.ts The name in brackets runs before the main handler.

Example: Body Parser Middleware

From the sample extension:
extensions/sample/src/api/createFoo/

Complete API Endpoint Example

Here’s the full structure for the createFoo endpoint:

Middleware Chain Examples

Single Middleware

No dependencies, just a simple handler.

Two Middleware

Execution order:
  1. validateInput.ts - Validates request data
  2. [validateInput]createOrder.ts - Creates the order

Three Middleware

Execution order:
  1. authenticate.ts - Verify user is logged in
  2. [authenticate]validatePermissions.ts - Check user has permission
  3. [authenticate][validatePermissions]updateProduct.ts - Update the product
Middleware in brackets must exist as separate files. The files are executed in dependency order, not alphabetically.

Request and Response Types

EverShop provides TypeScript types for Express request and response objects:

Common Patterns

Public API Endpoint

route.json
Anyone can access this endpoint without authentication.

Private API Endpoint

route.json
Requires authentication. EverShop automatically checks for valid session/token.

RESTful Resource

Page Routing

While API endpoints use route.json, page components use a different system based on directory structure and the layout export.

Page Areas

Pages are organized by “areas” - logical groupings based on where/when they render:

Layout Export

The layout export determines where a component renders:
Lower sortOrder numbers render first. Use multiples of 10 (10, 20, 30) to leave room for insertions.

Common Area IDs

Best Practices

Name your API directories and files clearly:
  • Good: createOrder, updateProduct, deleteUser
  • Bad: handler, endpoint1, api
Create separate validation middleware:
Return appropriate HTTP status codes:
Always import and use EverShop types:

Testing Routes

Using curl

Using Postman or Insomnia

  1. Set the HTTP method (GET, POST, etc.)
  2. Enter the URL: http://localhost:3000/foos
  3. Add headers (e.g., Content-Type: application/json)
  4. Add body (for POST/PUT requests)
  5. Send the request

Next Steps

  • Learn about Extensions for organizing your code
  • Understand GraphQL API for alternative data fetching
  • Explore Themes for page component rendering