I’m following a tutorial from youtube creating a simple dockerized CRUD application in Express, Node.js, Mongo & Redis. I stucked at video 3:06:57 on Authentication with sessions & Redis.
The initial issue I faced below when I send a 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...
I found out that the issue above was due to Redis version, the tutorial video was using Redis 3.0+ if not mistaken , but I’m having Redis 6.0+ .So I added the line redisClient.connect().catch(console.error);
but second issue happen.
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
}
the source files as below:
index.js
:
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);});
docker-compose.yml
:
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:
docker-compose.dev.yml
:
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
config.js
:
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
}
I did a check to the redis container logs and it’s up and running. Also it should be in the same network with the node-app.
docker command : docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
Full source code from tutor https://github.com/Sanjeev-Thiyagarajan/node-docker
2
Answers
Redis client doesn’t work in v4 of Redis. Replace the redisClient code with this:
I’ve tried this code and it solves the problem at 100%.