I am simply connecting redis server from docker compose networks but its showing timeout error. I tried with localhost etc but its not working with that also. Here is my app.js
import express from "express";
import { createClient } from 'redis';
const app = express();
const port = process.env.PORT || 3000;
const apiPrefix = process.env.API_PRIFEX;
const client = await createClient(
{
url: 'redis://redis:6379'
})
on('error', err => console.log('Redis Client Error', err))
.connect();
client.set('visits', 0);
app.get("/", (req, res) => {
client.get('visits', (err, visits) => {
res.send("Number of visits is " + visits)
client.set('visits', parseInt(visits) + 1);
})
res.status(200).json({ success: true, message: "Server1 running." });
});
app.listen(port, () => {
console.log(service is up and running on Port:${port});
});
But the issue is that it shows: connect ECONNREFUSED 127.0.0.1:6379
My docker-compose.yml looks like this I try with port and without port as well but nothing is working.
version: "3"
services:
redis:
image: "redis"
ports:
- "6379:6379"
node-app:
build: .
ports:
- "4001:3000"`
I have tried with different host name local host etc but in answers I have find I need to connect only with image name but still its not working.