如何从 Windows 机器上的 expo 服务器连接到 WSL

How to connect to WSL from expo server on Windows machine

环境

Windows10 家
React Native(在 windows 机器上,不是 WSL)
Android 工作室 4.0
虚拟设备信息
名称:Nexus S API 30
目标:Android 10.0+(Google APIs)
CPU/ABIx86

在 WSL2 端口 3000 中,Rails 作为 API 服务器

问题

当我从android工作室使用axios到WSL上的Rails服务器时,出现超时错误甚至没有Rails的日志。

const { rooms } = await createWrapper().post('/hoge/hage');
//createWrapper
import Axios from "axios";
import {camelizeKeys} from "humps";

export function createWrapper({
  isMultipart = false,
} = {}): any {
  const config: any = {
    headers: {
      Accept: "application/json",
      "Content-Type": isMultipart
        ? "multipart/form-data"
        : "application/json",
      // 'X-CSRF-Token': isCsrf ? getCsrfToken() : "",
    },
    baseURL: "http://10.0.2.2:3000",
    // ↑ ***** IPv4 address of WSL also doesnt work *****
    timeout: 300000,
    withCredentials: true,
  };

  const wrapper: any = Axios.create(config);

  wrapper.interceptors.response.use(
    (response: any): any => {
      return {
        ...response,
        data: camelizeKeys(response.data),
      };
    },
    (error: any): any => {
      if (error.code === "ECONNABORTED") {
        return Promise.reject({
          ...error,
          response: {
            data: {
              error_messages: ["timeout"],//***** RESULT HERE *****
            },
          },
        });
      } else if (!error.response) {
        return Promise.reject({
          ...error,
          response: {
            data: {
              error_messages: ["system error"],
            },
          },
        });
      }
      return Promise.reject(error);
    }
  );
  return wrapper;
}

检查WSL的IP地址

ifconfig | grep 'inet ' | awk '{print }'

exec Powershell(管理员),connectaddr 是 ifconfig 的结果。
现在端口设置 3000

netsh interface portproxy add v4tov4 listenport=3000 connectaddr=xxx.xx.xxx.xxx connectport=3000

确认

netsh interface portproxy show v4tov4