Backend
About Routers
A router in JSandy is a collection of procedures (API endpoints) related to a specific feature or resource. For example:
userRouterfor user management operationspostRouterfor blog post operationspaymentRouterfor payment related endpoints
app/
└── server/
├── jsandy.ts # Initializing JSandy
├── index.ts # Main appRouter
└── routers/ # Router directory
├── user-router.ts
├── post-router.ts
└── payment-router.tsCreating a Router
-
Create a new file in
server/routers:import { j } from "../jsandy" export const postRouter = j.router({ // Procedures go here... }) -
Add procedures to your router:
import { j, publicProcedure } from "../jsandy" export const postRouter = j.router({ list: publicProcedure.get(({ c }) => { return c.json({ posts: [] }) }), create: publicProcedure.post(({ c }) => { return c.json({ success: true }) }), }) -
Register your router with the main
appRouter:import { j } from "./jsandy" import { postRouter } from "./routers/post-router" const api = j .router() .basePath("/api") .use(j.defaults.cors) .onError(j.defaults.errorHandler) const appRouter = j.mergeRouters(api, { post: postRouter, }) export type AppRouter = typeof appRouter export default appRouter
Under the hood, each procedure is a separate HTTP endpoint. The URL structure is as follows:
- The base path of your API (
/api) - The router name (
post) - The procedure name (
list)
For example, the list procedure is now available at
http://localhost:3000/api/post/list