Redis在docker中未定义

Max*_*ax 1 redis node.js docker graphql

我正在构建 GraphQL API,并且想对某些解析器进行速率限制。为此,我使用此速率限制器: https: //github.com/teamplanes/graphql-rate-limit

这就是我的实现方式:

import { createRateLimitRule, RedisStore } from 'graphql-rate-limit';
import redis from 'redis'

const rateLimit = createRateLimitRule({ 
  identifyContext: (ctx) => ctx.req.ip, 
  formatError: () => `Hey there, you are doing way too much`,
  store: new RedisStore(redis.createClient(6379, 'redis'))
});
Run Code Online (Sandbox Code Playgroud)

但是当我运行时,docker-compose up我的 NodeJS 应用程序出现此错误:
TypeError: Cannot read property 'createClient' of undefined

我的 PostgreSQL 数据库和 NodeJS 应用程序都正常启动。

这是我的 docker-compose 文件:

version: "3.7"
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 4000:4000
    depends_on: 
      - postgres
      - redis
    env_file:
      - .env

  postgres:
    image: postgres
    ports:
      - 5432:5432
    env_file:
      - .env
    volumes:
      - ./init-schema.sql:/docker-entrypoint-initdb.d/init.sql
      - postgres:/var/lib/postgresql/data

  redis:
    image: redis
    command: ["redis-server", "--bind", "0.0.0.0", "--port", "6379"]
    hostname: redis
    volumes:
      - redis_data:/data
    ports:
      - 6379:6379

volumes: 
  redis_data:
    name: redis_data
  postgres:
    name: project_db
Run Code Online (Sandbox Code Playgroud)

还有我的 NodeJS 文件的 Dockerfile:

FROM node:12 AS builder
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma/

RUN npm install
RUN npx prisma generate

COPY . .

RUN npm run build

FROM node:12

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/api ./api

EXPOSE 4000
CMD ["npm", "run", "dev"]
Run Code Online (Sandbox Code Playgroud)

另外,当我删除 Redis 时,一切正常。所以请帮助我。

and*_*anc 5

它说redisvar 未定义。您可能需要:

import * as redis from 'redis'
Run Code Online (Sandbox Code Playgroud)

或者

const redis = require('redis')
Run Code Online (Sandbox Code Playgroud)

并确保您已安装该软件包:

npm install redis
Run Code Online (Sandbox Code Playgroud)