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

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>

Related

Railway error deploying my server with nodejs saying hostname: 'undefined'?

This is on theRailway console:
original: Error: getaddrinfo ENOTFOUND undefined
original: Error: getaddrinfo ENOTFOUND undefined
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26) {
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26) {
errno: -3008,
errno: -3008,
code: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
syscall: 'getaddrinfo',
hostname: 'undefined'
hostname: 'undefined'
I follow all the steps to deploy my full-stack app on railway. I first, deployed my db from postgress successfully and pass it the links to my .env on my server side app. Now, looks like the hostname is not working not sure why is this happening.

Why is the error displayed when starting the application on expreess js: getaddrinfo ENOTFOUND localhostss

When I try to run the hosted API app, I get the following message:
2022-04-11T12:32:31: Server running at http://localhost:9000
2022-04-11T12:32:31: Error: getaddrinfo ENOTFOUND localhostss
2022-04-11T12:32:31: at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
2022-04-11T12:32:31: errno: -3008,
2022-04-11T12:32:31: code: 'ENOTFOUND',
2022-04-11T12:32:31: syscall: 'getaddrinfo',
2022-04-11T12:32:31: hostname: 'localhostss',
2022-04-11T12:32:31: fatal: true
2022-04-11T12:32:31: }
Please help me fix
index.js:
enter image description here
hostname is wrong
according to error hostname is localhostss rather than localhost

Redis connection error while connecting to 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
}

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
}

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