micro-analytics / micro-analytics-cli

Public analytics as a Node.js microservice. No sysadmin experience required! 📈
MIT License
734 stars 39 forks source link

CORS support #78

Closed joshwcomeau closed 6 years ago

joshwcomeau commented 6 years ago

Hi there! Thanks for the great tool, was delighted when Max told me this existed :)

I've run it on my digitalocean droplet, but I'd like to use it for sites on Github Pages. When I try to make a fetch request for it, I get a CORS error:

Cross-origin redirection to https://server-ip-address/my-path denied by Cross-Origin Resource Sharing policy: Origin http://localhost:3002 is not allowed by Access-Control-Allow-Origin.

It looks like a PR was opened last year, #17, which fixed this by adding the right header. Looking through the current source, though, I don't see that header being sent. Was it intentionally removed? If so, is there a way to specify allowed origins that I'm missing?

Happy to open a quick PR if necessary!

joshwcomeau commented 6 years ago

Oh, hm, I spoke too soon; the header still exists. Not sure why it isn't working for me!

Will dig into it a bit.

If it helps, here's my request code, on localhost:

  componentDidMount() {
    const url = `http://server-ip-address${window.location.pathname}`;

    fetch(url)
      .then(response => response.json())
      .then(({ views }) => this.setState({ hits: views }))
      .catch(err => console.log('Error retrieving hit counter stats:', err));
  }
joshwcomeau commented 6 years ago

Ah-ha! Figured it out, I wasn't specifying the port in server-ip-address. Without the port it was trying to access the server's main page, which is HTTPS, and that redirection was failing.