Define routes and attach endpoints to your API
Routing
import { Routing } from "express-zod-api"; const routing: Routing = { v1: { users: { get: listUsers, post: createUser, ":id": { get: getUser, patch: updateUser, delete: deleteUser, }, }, }, };
const routing: Routing = { api: { v1: { users: listUsersEndpoint, }, }, }; // Route: GET /api/v1/users
const routing: Routing = { "/api/v1/users": listUsersEndpoint, }; // Route: GET /api/v1/users
const routing: Routing = { users: { ":id": getUserEndpoint, }, }; // Route: GET /users/:id
const routing: Routing = { users: { get: listUsers, post: createUser, }, }; // Routes: GET /users, POST /users
const routing: Routing = { "post /users": createUserEndpoint, "get /users/:id": getUserEndpoint, };
import { ServeStatic } from "express-zod-api"; const routing: Routing = { public: new ServeStatic("./assets", { dotfiles: "deny", index: false, }), }; // Serves files from ./assets at /public
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"), };
const parentEndpoint = defaultEndpointsFactory.build({ ... }); const childEndpoint = defaultEndpointsFactory.build({ ... }); const routing: Routing = { parent: parentEndpoint.nest({ child: childEndpoint, }), }; // Routes: /parent and /parent/child