TeixeiraT
tRPC12mo ago
3 replies
Teixeira

Route not found when using Fastify adapter

I'm currently running into the following error when attempting to issue a request to my server which is leveraging Fastify:
[19:04:02.888] INFO (57024): Route POST:/associateSocialMediaAccount not found
    reqId: "req-1"
[19:04:02.892] INFO (57024): request completed
    reqId: "req-1"
    res: {
      "statusCode": 404
    }
    responseTime: 5.059000000357628


I'm instantiating the server as such:
const fastifyServer = fastify({
    logger: true,
    maxParamLength: 5000
});

fastifyServer.register(fastifyTRPCPlugin, {
    prefix: '/trpc',
    trpcOptions: {
        router: appRouter,
        createContext,
        onError({ path, error }) {
            logger.error(`Error in tRPC handler on path '${path}':`, error);
        }
    } satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions']
});

try {
    await fastifyServer.listen({
        port: environment.API_PORT,
        host: environment.isDev ? 'localhost' : '0.0.0.0'
    });
} catch (reason: unknown) {
    logger.error(reason);
}


appRouter looks like this:
(...)
import associateSocialMediaAccount from './procedures/associateSocialMediaAccount.js';

export const appRouter = router({
    associateSocialMediaAccount
});


associateSocialMediaAccount.ts:
export default publicProcedure
    .input(socialMediaAccountSchema)
    .mutation(async (opts) => {
        // ...
    });


I have triple checked that the client and server versions of tRPC match ( ^11.0.0-rc.700), I'm on "typescript": "^5.7.3" on both server and client, Node version v22.11.0
Solution
The client side url was missing the /trpc prefix set in the server.
Was this page helpful?