TanStackT
TanStack12mo ago
4 replies
forward-apricot

Using redis in registerGlobalMiddleware

Hello everyone,

I don't know if I am using the registerGlobalMiddleware incorrectly but I cannot make redis work with it for some reason.

With the very simple code below I keep getting the error attached. Has anyone else faced a similar issue with the registerGlobalMiddleware? Any guidance or suggestions would be greatly appreciated!

I am also not seeing any of the logs, even with a simple middleware that just logs somehting to the console

This error can be reproduced with the following code:

__root
import { rateLimiterMiddleware } from "~/libs/cache.lib";
import { registerGlobalMiddleware } from "@tanstack/start";

registerGlobalMiddleware({
  middleware: [rateLimiterMiddleware],
});


~/libs/cache.lib
import { createMiddleware } from "@tanstack/start";
import { Redis } from "ioredis";

export const redis = new Redis({
  username: "default",
  password: "secretPassword!",
  host: "localhost",
  port: 6379,
  name: "0",
});

export const rateLimiterMiddleware = createMiddleware().server(
  async ({ next }) => {
    const res = await redis.get("hello");

    console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    console.log(res);
    console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

    return await next();
  },
);


docker-compose.yaml
services:
  cache:
    image: redis:latest
    ports:
      - 6379:6379
image.png
Was this page helpful?