redis在docker中未定义

ygya80vv  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(359)

我正在构建一个graphqlapi,我想对一些解析器进行速率限制。为此,我使用了这个速率限制器: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'))
});

但当我跑的时候 docker-compose up 我从nodejs应用程序中得到以下错误: TypeError: Cannot read property 'createClient' of undefined 我的postgresql数据库和nodejs应用程序都正常启动。
这是我的docker撰写文件:

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

以及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"]

而且,当我删除redis时,一切都正常。所以请帮帮我。

kadbb459

kadbb4591#

据说 redis var未定义。您可能需要:

import * as redis from 'redis'

const redis = require('redis')

还要确保安装了软件包:

npm install redis

相关问题