我正在关注 youtube 上的教程,在 Express、Node.js、Mongo 和 Redis 中创建一个简单的 dockerized CRUD 应用程序。我停留在视频 3:06:57 上使用会话和 Redis 进行身份验证。
我在下面发送 POST 时遇到的最初问题
ClientClosedError: The client is closed
at Commander._RedisClient_sendCommand (/app/node_modules/@node-redis/client/dist/lib/client/index.js:408:31)
at Commander.commandsExecutor (/app/node_modules/@node-redis/client/dist/lib/client/index.js:166:154)
at Commander.BaseClass.<computed> [as set] (/app/node_modules/@node-redis/client/dist/lib/commander.js:8:29)
at RedisStore.set (/app/node_modules/connect-redis/lib/connect-redis.js:65:21)
at Session.save (/app/node_modules/express-session/session/session.js:72:25)
at Session.save (/app/node_modules/express-session/index.js:406:15)
at ServerResponse.end (/app/node_modules/express-session/index.js:335:21)
at ServerResponse.send (/app/node_modules/express/lib/response.js:221:10)
at ServerResponse.json (/app/node_modules/express/lib/response.js:267:15)
at exports.login (/app/controllers/authController.js:45:28)
[nodemon] app crashed - waiting for file changes before starting...
我发现上面的问题是由于 Redis 版本造成的,如果没有弄错,教程视频使用的是 Redis 3.0+,但我使用的是 Redis 6.0+。所以我添加了该行redisClient.connect().catch(console.error);
但发生了第二个问题。
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1138:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
源文件如下:
const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const redis = require("redis");
let RedisStore = require("connect-redis")(session);
const { MONGO_PASSWORD, MONGO_IP, MONGO_PORT, MONGO_USER, REDIS_URL, REDIS_PORT, SESSION_SECRET } = require("./config/config");
let redisClient = redis.createClient({
host: REDIS_URL,
port: REDIS_PORT,
})
// let redisClient = createClient({
// host: REDIS_URL,
// port: REDIS_PORT
// });
//redisClient.connect().catch(console.error);
redisClient.on('error', err => {console.log('Error ' + err);});
version : "3"
services :
node-app:
build : .
ports:
- "3000:3000"
environment:
- PORT=3000
depends_on:
- mongo
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- mongo-db:/data/db
redis:
image: redis
volumes:
mongo-db:
version : "3"
services :
node-app:
build:
context: .
args:
NODE_ENV: development
volumes:
- ./:/app
- /app/node_modules
environment:
- NODE_ENV=development
- MONGO_USER=admin
- MONGO_PASSWORD=password
- SESSION_SECRET=secret
command: npm run dev
mongo:
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
module.exports = {
MONGO_IP: process.env.MONGO_IP || "mongo",
MONGO_PORT: process.env.MONGO_PORT || 27017,
MONGO_USER: process.env.MONGO_USER,
MONGO_PASSWORD: process.env.MONGO_PASSWORD,
REDIS_URL: process.env.REDIS_URL || "redis",
REDIS_PORT: process.env.REDIS_PORT || 6379,
SESSION_SECRET: process.env.SESSION_SECRET
}
我检查了 redis 容器日志,它已启动并正在运行redis-container logs。它也应该与节点应用程序在同一个网络中。
码头工人命令:docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
提前感谢您的协助。
来自导师的完整源代码https://github.com/Sanjeev-Thiyagarajan/node-docker