JSandy Logo

JSandy

docs

Backend

About Routers

A router in JSandy is a collection of procedures (API endpoints) related to a specific feature or resource. For example:

  • userRouter for user management operations
  • postRouter for blog post operations
  • paymentRouter for payment related endpoints
app/
  └── server/
      ├── jsandy.ts        # Initializing JSandy
      ├── index.ts         # Main appRouter
      └── routers/         # Router directory
          ├── user-router.ts
          ├── post-router.ts
          └── payment-router.ts

Creating a Router

  1. Create a new file in server/routers:

    import { j } from "../jsandy"
    
    export const postRouter = j.router({
      // Procedures go here...
    })
  2. 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 })
      }),
    })
  3. 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

On this page