我在试着和你联系 redis
(指docker示例)使用 express(node.js)
. 这是我的 index.ts
我正在尝试连接的文件 redis
```
import mongoose from 'mongoose'
import express from 'express'
import session from 'express-session'
import connectRedis from 'connect-redis'
import Redis from 'ioredis'
import { MONGO_URI, MONGO_OPTIONS, REDIS_OPTIONS, APP_PORT } from './config'
import { createApp } from './app';
; (async () => {
await mongoose.connect(MONGO_URI, MONGO_OPTIONS)
const RedistStore = connectRedis(session)
const client = new Redis(REDIS_OPTIONS)
const store = new RedistStore({ client })
const app = createApp(store)
app.listen(APP_PORT, () => console.log('server running on port 3000'))
})()
这是我的 `REDIS_OPTIONS` 我所有的文件 `redis` 选项。
import { RedisOptions } from 'ioredis'
const {
REDIS_PORT = 6379,
REDIS_HOST = 'localhost',
REDIS_PASSWORD = 'secret'
} = process.env
export const REDIS_OPTIONS: RedisOptions = {
port: +REDIS_PORT,
host: REDIS_HOST,
password: REDIS_PASSWORD
}`docker-compose.yml`
version: '3'
services:
db:
user: $UID
image: mongo
ports:
- '27017:27017'
environment:
MONGO_INITDB_ROOT: ''
MONGO_INITDB_PASSWORD: ''
MONGO_INITDB_DATABASE: auth,
MONGO_USERNAME: ''
MONGO_PASSWORD: ''
volumes:
- ./data:/data/db
- ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
cache:
image: redis:alpine
ports:
- '6379:6379'
command: ['--requirepass "secret"']`package.json`
{
"name": "node-auth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"up": "docker-compose up -d",
"postup": "npm run dev",
"stop": "docker-compose stop",
"down": "docker-compose down",
"dev": "npm run dev --prefix api"
},
"keywords": [],
"author": "",
"license": "ISC"
}
现在,当我试图运行我的主应用程序文件,所以我得到这个错误
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) node.js
我怎样才能摆脱这个错误?
2条答案
按热度按时间3npbholx1#
看起来redis容器可能有问题。您使用什么命令来运行容器?尝试添加
-p 6379:6379
用你的码头指挥。另外,检查端口6379是否被其他人使用或被防病毒或防火墙阻止。wrrgggsh2#
将index.ts中的主机从“localhost”更改为“redis”