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

1

10

Hey I'm working on SSR app with express. when the request hits, render server(running on localhost:3000) send request to api server(running on localhost:3001) for initial user's session store. but when sending a request it throws this Unknown error at this loadFromSessionStorage function

Part of my route handler. it throws error on axios.get request

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
    }
  }

and catched err object looks like this enter image description here

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"

I have no idea about this error. I searched for that and I got this answer and tried this. https://github.com/axios/axios/issues/1514 it's basically saying that set my environment variable http_proxy , https_proxy to "" and I did it with my .bash and .zshrc config and tried

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

too. but its still throwing this error during request. this is my browser's(chrome) developer tool's request and response on that request

Request

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

and Response

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

any ideas why it occurs ?? and I couldn't fix with it but I also wondering why we have to set http_proxy environment variable for this issue? since its just request between another port, I guess proxy is nothing to do with that. any explanation or ideas would be appreciated

Sheelagh answered 2/3, 2019 at 15:13 Comment(0)
H
35

Have you tried putting an http:// in front of localhost in the Axios request?

Hitandmiss answered 2/3, 2019 at 16:54 Comment(1)
I believe this is because Axios by default assumes that you're using the same domian that it is installed on using a relative URL. So if you want to make the request to a different domain (i.e. your API server) you need to include the http:// to signal the absolute URL.Hitandmiss

© 2022 - 2024 — McMap. All rights reserved.