Skip to main content

Overview

The Routing type defines the URL structure of your API by mapping paths to endpoints.
import { Routing } from "express-zod-api";

const routing: Routing = {
  v1: {
    users: {
      get: listUsers,
      post: createUser,
      ":id": {
        get: getUser,
        patch: updateUser,
        delete: deleteUser,
      },
    },
  },
};

Routing Syntax

Nested Objects

const routing: Routing = {
  api: {
    v1: {
      users: listUsersEndpoint,
    },
  },
};
// Route: GET /api/v1/users

Flat Paths

const routing: Routing = {
  "/api/v1/users": listUsersEndpoint,
};
// Route: GET /api/v1/users

Path Parameters

const routing: Routing = {
  users: {
    ":id": getUserEndpoint,
  },
};
// Route: GET /users/:id

Method-Based Routing

const routing: Routing = {
  users: {
    get: listUsers,
    post: createUser,
  },
};
// Routes: GET /users, POST /users

Explicit Method in Path

const routing: Routing = {
  "post /users": createUserEndpoint,
  "get /users/:id": getUserEndpoint,
};

Static File Serving

import { ServeStatic } from "express-zod-api";

const routing: Routing = {
  public: new ServeStatic("./assets", {
    dotfiles: "deny",
    index: false,
  }),
};
// Serves files from ./assets at /public

Complex Example

import { Routing, ServeStatic } from "express-zod-api";

const routing: Routing = {
  // Flat syntax
  "/health": healthCheckEndpoint,
  
  // Nested API structure
  api: {
    v1: {
      // Method-based
      users: {
        get: listUsers,
        post: createUser,
        ":id": {
          get: getUser,
          patch: updateUser,
          delete: deleteUser,
        },
      },
      
      // Posts with nested comments
      posts: {
        get: listPosts,
        ":postId": {
          get: getPost,
          comments: {
            get: listComments,
            post: createComment,
          },
        },
      },
      
      // Explicit method
      "post /auth/login": loginEndpoint,
      "post /auth/logout": logoutEndpoint,
    },
  },
  
  // Static files
  static: new ServeStatic("./public"),
};

Endpoint Nesting

Endpoints can handle both their own path and sub-paths:
const parentEndpoint = defaultEndpointsFactory.build({ ... });
const childEndpoint = defaultEndpointsFactory.build({ ... });

const routing: Routing = {
  parent: parentEndpoint.nest({
    child: childEndpoint,
  }),
};
// Routes: /parent and /parent/child

See Also