2 ip in 1 request with axio on docker/kubernetes

6/3/2020

I am making an application with docker and orchestrator kubernetes. In the fronted it is made with create-react-app, the problem comes when I want to use axios I get 2 ip in a request.

the code below is the action of the redux to capture the data

import { LOGIN, LOGIN_EXITO, LOGIN_ERROR } from "../types";

// import clienteAxios from "../../axios";
import axios from "axios";
import Swal from "sweetalert2";

const HOST = window._env_.REACT_APP_HOST_BACKEND;
const PORT = window._env_.REACT_APP_PORT_BACKEND;

export function loginAction(user, password) {
  return async (dispatch) => {
    dispatch(login());

    try {
      // let datos = await clienteAxios.post("/login", { user, password });
      console.log(`${HOST}:${PORT}/api`);
      let datos = await axios.post(`${HOST}:${PORT}/api`, { user, password });
      console.log(datos);

      dispatch(loginExito(datos.data));

      if (datos.data[0].r_mensaje === "ok") {
        Swal.fire("Acceso...", "Bienvenido", "success");
      } else {
        Swal.fire({
          icon: "error",
          title: "Error...",
          text: datos.data[0].r_mensaje,
        });
      }
    } catch (error) {
      console.log(error);
      dispatch(loginError(true));

      Swal.fire({
        icon: "error",
        title: "Error...",
        text: "Intente de nuevo...",
      });
    }
  };
}

and when making the query this error throws me, that 2 ip join enter image description here

this is my nginx.conf that I use in the dockerfile.

server {

  listen 3000;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  error_page   500 502 503 504  /50x.html;

  location = /50x.html {
    root   /usr/share/nginx/html;
  }

}
-- Silvio Peña
axios
docker
kubernetes
nginx
reactjs

1 Answer

6/3/2020

Axios is interpreting your argument as a relative path and is automatically appending the scheme, host and port to form the request URL.

For example, axios.post('/user', ...), would translate to http://172.17.0.3:30599/user.

You can fix this by passing the full URL:

axios.post(`http://${HOST}:${PORT}/api`, ...)

Alternatively, create an axios instance, set baseURL in the config, and use instance.post('/api').

-- chash
Source: StackOverflow