TanStackT
TanStack7mo ago
4 replies
sacred-rose

createServerFileRoute with different middleware for GET and POST

I am trying to create an app with an API that uses a different middleware for GET and POST. I am using the example code from the docs.

My code looks like this
export const ServerRoute = createServerFileRoute(
  "/api/actor/$id"
).methods((api) => ({
  GET: api
    .middleware([checkApiAccess(AccessLevel.Read)])
    .handler(async ({ params }) => {
      let companies = await getActor(params.id);
      return json(companies);
    }),
}));


I have removed the POST for brevity. When I use this code, I get the error TypeError: handler is not a function. Can somebody explain what I am doing wrong because I feel that having different middlewares to control access to GET and POST endpoints should be a regular use-case.
// TODO: Add redirect from api-routes to server-routes Server routes are a powerful feature of TanStack Start that allow you to create server-side endpoints in your application and are useful for hand...
Server Routes | TanStack Start React Docs
Was this page helpful?