axios 请求获取连接 EHOSTUNREACH 错误。响应 header 得到 default-src 'self'

axios request get connect EHOSTUNREACH Error. response header got default-src 'self'

嘿,我正在使用 express 开发 SSR 应用程序。当请求命中时,渲染服务器(运行 on localhost:3000)将请求发送到 api 服务器(运行 on localhost:3001)以获取初始用户的 session店铺。但是在发送请求时,它会在此 loadFromSessionStorage 函数中抛出此未知错误

我的路线处理程序的一部分。它在 axios.get 请求

上抛出错误
const isServer = (typeof window === 'undefined') || (!window.document);

  if (isServer) {
    axios.defaults.headers = { cookie: req.get('cookie') || '' };
  }

  const loadFromSessionStorage = async () => {
    try {
      const res = await axios.get(`localhost:3001/session`)
      if (res) {
        return {
          user: res.data
        }
      }
    } catch (err) {
      if (err.response.data.sessionNotFound) {
        return {};
      }
      console.log("Unknown error from loading session storage")
      console.log(err);
      return;
      // TODO: 500
    }
  }

并发现错误 object 看起来像这样

Error: connect EHOSTUNREACH 0.0.11.185:80 - Local (192.168.219.101:56794)

address:"0.0.11.185"
code:"EHOSTUNREACH"
config:Object {adapter: , transformRequest: Object, transformResponse: Object, …}
errno:"EHOSTUNREACH"
message:"connect EHOSTUNREACH 0.0.11.185:80 - Local (192.168.219.101:56794)"
port:80
request:RedirectableRequest {_writableState: WritableState, writable: true, _events: Object, …}
response:undefined
stack:"Error: connect EHOSTUNREACH 0.0.11.185:80 - Local (192.168.219.101:56794)\n    at internalConnect (net.js:857:16)\n    at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)\n    at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1004:9)\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:62:10)"
syscall:"connect"

我不知道这个错误。我搜索了那个,得到了这个答案并尝试了这个。 https://github.com/axios/axios/issues/1514 它基本上是说将我的环境变量 http_proxy , https_proxy 设置为“”,我用我的 .bash 和 .zshrc 配置做了它并尝试了

npm config delete http-proxy
npm config delete https-proxy

也是。但它仍然在请求期间抛出此错误。这是我的浏览器(chrome)开发者工具对该请求的请求和响应

请求

GET / HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
Cookie: connect.sid=s%3AFLr3gvuCNhnyuDPciCFDIg9JFOjU blah blah

和响应

HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Content-Security-Policy: default-src 'self'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 398
Date: Sat, 02 Mar 2019 14:48:25 GMT
Connection: keep-alive

知道为什么会发生吗??我无法解决它,但我也想知道为什么我们必须为此问题设置 http_proxy 环境变量?因为它只是在另一个端口之间请求,所以我猜代理与此无关。任何解释或想法将不胜感激

您是否尝试过在 Axios 请求中将 http:// 放在 localhost 前面?