Redis connection error while connecting to Node js - node.js

I am trying to connect the node js container with the Redis container, I am facing an issue when I run the node-js container and try to connect Redis. I tried most of the solutions from the web, still not able to fix the connection refused issuer.
> Node js creating the Redis client: below code works locally good, without the container.
const redis = require('redis')
const redisClient = redis.createClient({
host: "127.0.0.1",
port: "6379"
})
> running Redis from the latest image.
docker run -d --name redis -p 6379:6379 redis
> Error:
[nodemon] starting `node index.js`
events.js:377
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16)
Emitted 'error' event on RedisClient instance at:
at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:342:14)
at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:223:14)
at Socket.emit (events.js:400:28)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}

Related

CloudWatch "UnknownEndpoint" error when running Node/express app to EC2 using Docker

I try to run a node/express app on ec2 using a docker image with this command "sudo docker run -it -p 127.0.0.1:3000:3000 name/app:latest" and I receive the following error:
UnknownEndpoint: Inaccessible host: `logs.us-east-1.amazonaws.com’ at port `undefined’. This service may not be available in the `us-east-1' region.
at Request.ENOTFOUND_ERROR (/usr/app/node_modules/aws-sdk/lib/event_listeners.js:574:46)
at Request.callListeners (/usr/app/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/usr/app/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/usr/app/node_modules/aws-sdk/lib/request.js:686:14)
at error (/usr/app/node_modules/aws-sdk/lib/event_listeners.js:406:22)
at ClientRequest.<anonymous> (/usr/app/node_modules/aws-sdk/lib/http/node.js:99:9)
at ClientRequest.emit (node:events:527:28)
at ClientRequest.emit (node:domain:475:12)
at TLSSocket.socketErrorListener (node:_http_client:454:9)
at TLSSocket.emit (node:events:527:28)
Emitted ‘error’ event on DerivedLogger instance at:
at DerivedLogger.transportEvent (/usr/app/node_modules/winston/lib/winston/logger.js:639:12)
at CloudWatchTransport.emit (node:events:539:35)
at CloudWatchTransport.emit (node:domain:475:12)
at Relay.<anonymous> (/usr/app/node_modules/winston-aws-cloudwatch/lib/index.js:15:41)
at Relay.emit (node:events:527:28)
at Relay.emit (node:domain:475:12)
at Relay._onError (/usr/app/node_modules/winston-aws-cloudwatch/lib/relay.js:74:12)
at /usr/app/node_modules/winston-aws-cloudwatch/lib/relay.js:53:57
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: ‘UnknownEndpoint’,
region: ‘us-east-1’,
hostname: ‘logs.us-east-1.amazonaws.com’,
retryable: true,
originalError: Error: getaddrinfo EAI_AGAIN logs.us-east-1.amazonaws.com
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3001,
code: ‘NetworkingError’,
syscall: ‘getaddrinfo’,
hostname: ‘logs.us-east-1.amazonaws.com’,
region: ‘us-east-1’,
retryable: true,
time: 2022-11-17T15:06:46.985Z
},
time: 2022-11-17T15:06:46.985Z
}
AWS AMI: ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20220912
"#aws-sdk/client-cloudwatch-logs": "^3.183.0",
HTTPS is enabled on server
If I clone the repo on ec2 and run it using "pm2 --name appName start npm -- start" it works well.
Can you help me to understand how to fix this problem?
I fixed this problem by using the docker host network with the following flag docker --network host as described here.

How to implement redis cache in node.js project in Windows

I have implemented redis cache for get method, but if Redis server is closed, my node.js project doesn't start again. If the Redis server is running everything runs fine. What should I do?
I get this error when I start my project without Redis server:
PS D:\MyWorkSpace\PAS DEV BRANCH\cs_app_api> npm run dev
> dev
> ts-node ./src/server.ts
App running on the port 8081
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
PS D:\MyWorkSpace\PAS DEV BRANCH\cs_app_api>

Redis disconnects randomly when running on WSL but works perfectly when run on Linux server

I have node.js app that uses Redis as a cache among other things.
When my app is running on my Linux server it works great without any problems. But when I am running it locally on my windows machine through WSL it disconnects randomly but mostly when it idles.
it disconnects with this error:
Emitted 'error' event on RedisClient instance at:
at RedisClient.on_info_cmd (C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\index.js:431:14)
at C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\index.js:470:14
at Object.callbackOrEmit [as callback_or_emit] (C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\lib\utils.js:89:9)
at Command.callback (C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\lib\individualCommands.js:157:15)
at RedisClient.flush_and_error (C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\index.js:309:29)
at RedisClient.connection_gone (C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\index.js:602:14)
[... lines matching original stack trace ...]
at endReadableNT (node:internal/streams/readable:1307:12) {
code: 'UNCERTAIN_STATE',
command
}
And on rarer times with this error:
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1139:16)
Emitted 'error' event on RedisClient instance at:
at RedisClient.on_error (C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\index.js:341:14)
at Socket.<anonymous> (C:\Users\mynam\OneDrive\Desktop\nodeapp\node_modules\redis\index.js:222:14)
at Socket.emit (node:events:379:20)
at emitErrorNT (node:internal/streams/destroy:188:8)
at emitErrorCloseNT (node:internal/streams/destroy:153:3)
at processTicksAndRejections (node:internal/process/task_queues:81:21) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}

API stress test locally got ECONNREFUSED

I'm doing a stress test to my nodejs app, the test basically send a bulk of requests concurrently, sometimes I got this annoying error:
{ RequestError: Error: connect ECONNREFUSED 127.0.0.1:1234
at new RequestError (/mnt/c/Users/rawhi/Desktop/myCode/backend/api/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/mnt/c/Users/rawhi/Desktop/myCode/backend/api/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/mnt/c/Users/rawhi/Desktop/myCode/backend/api/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/mnt/c/Users/rawhi/Desktop/myCode/backend/api/node_modules/request/request.js:185:22)
at Request.emit (events.js:198:13)
at Request.onRequestError (/mnt/c/Users/rawhi/Desktop/myCode/backend/api/node_modules/request/request.js:881:8)
at ClientRequest.emit (events.js:198:13)
at Socket.socketErrorListener (_http_client.js:392:9)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'RequestError',
message: 'Error: connect ECONNREFUSED 127.0.0.1:1234',
cause:
{ Error: connect ECONNREFUSED 127.0.0.1:1234
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 1234 }
So the test keeps returning this error for "almost" all the requests, but I still can send requests via postman without any problem.
Can anyone explain me what is the problem here ? and how it can be solved ? like using a queue a proxy or anything else useful ?
Thanks in advance

Axios connecting me to wrong port

I am using axios as an HTTP client. When I try to POST on my local server I get this error message:
{ Error: connect ECONNREFUSED 127.0.0.1:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
config:{......................
.............................
.........}
I don't know what is wrong in my node js app. I am posting data to port 80(instead of to localhost' port) Does someone have any ideas?

Resources