enable CORS for local http server

Viewed 5155

I hosted my local files with http server. Just install on my Mac HTTP server with command:

npm install --global http-server

Then I start this server in needed directory with the command:

http-server

I had the CORS policy problem when trying to fetch my local HTML file from HTTP server response. It was blocked. Blocked by CORS policy

2 Answers

All I need to do for resolving this issue just to start HTTP server with cors option:

http-server --cors

Was unable to get http-server running with CORS support (different browsers, disabling cache... nothing helped).

As the related bug ticket is still an ongoing discussion, here is an alternative that worked for me out of the box using local-web-server:

  1. npm i local-web-server --global
  2. ws --cors.origin http://<your domain> --cors.credentials

You might add --https -p 443 if you need to simulate local SSL support.

Related