node.jstcpmicroservicesapi-gatewaykrakend

connection refused error with Krakend api-gateway?


I want to see and try how does api-gateway work specifically I am using Krakend. For technically, I have a service where it only receives a request and respond back. The problem is the service which is a very simple http-server does not receive the request from the api-gateway when the regarding endpoint is triggered through api-gateway. However, when I make a get request through browser writing the exact url http://localhost:8000/api/v1/users, I get the response. Basically, what I want is, when I make a get request to the localhost:8099/users which is the api-gateway I want api-gateway to make a get request to the users api and see the expected logs.

This is the error I get.

Error #01: Get "http://localhost:8000/api/v1/users": dial tcp 127.0.0.1:8000: connect: connection refused

Here is my krakend.json file where I put the configuration.

{
    "version": 2,
    "host" : [
        "http://localhost:8099"
    ],
    "extra_config": {
      "github_com/devopsfaith/krakend-cors": {
        "allow_origins": [
          "*"
        ],
        "expose_headers": [
          "Content-Length"
        ],
        "max_age": "12h",
        "allow_methods": [
          "GET"
        ],
        "allow_headers": [
          "*"
        ]
      }
    },
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "output_encoding": "string",
    "name": "api-gateway",
    "port": 8099,
    "read_timeout": "5s",
    "idle_timeout": "5s",
    "write_timeout": "5s",
    "read_header_timeout": "5s",
    "endpoints": [
      {
        "endpoint": "/users",
        "method" : "GET",
        "backend": [
          {
            "encoding" : "string",
            "url_pattern": "/api/v1/users",
            "method": "GET",
            "host": [
              "http://localhost:8000"
            ]
          }
        ]
      }
    ]
  }

And this is my node js server.

const express = require('express')
const app = express()
const cors = require('cors')

var corsOptions = {
    origin: 'http://localhost:8099',
    methods: "GET, PUT"
}

app.use(cors(corsOptions))

app.get('/api/v1/users', (req, res) => {
    console.log('REQUEST => \n')
    console.log(req.url)
    console.log(req.hostname)
})

app.listen(8000, () => {console.log('server has started on 8000')})

So, when I make a get request to the address http://localhost:8099/users the log is as follows:

[GIN] 2021/06/30 - 22:44:05 | 500 |      4.8998ms |      172.17.0.1 | GET      "/users"
Error #01: Get "http://localhost:8000/api/v1/users": dial tcp 127.0.0.1:8000: connect: connection refused

In the Krakend documentation here it says when status codes above 400 are returned from the backend, "I guess" the log is written as above with 500 code.

500 Internal Server Error Default error code, and in general, when backends return any status above 400

I did play with the krakend.json file and changed different parts of the file and still didn't work.

I know this is a tcp error checked what could be the reasons causing that error. 1- port may be crashed, which is not true because when I make a get request directly to the endpoint http://localhost:8000/api/v1/users I get the log.


Solution

  • For each service, the name of the container declared in the docker-compose.yml should match the name written in the krakend.json file. For instance, user-service should be also declared in krakend.json file as http://user-service:$PORT in the host field.