Simple CORS workaround for local development

Keep is Simple

Published June 15, 2020

javascript, cors, static_sites
Contents

I've been doing a lot of web development old school, just editing HTML and JavaScript by hand without a build environment. Running npx live-server is an easy one liner to spin up a server, that opens a browser for you and also updates changes on safe.

Sometimes that's not enough. Often you want to pull in data from an API, and that requires HTTPS, and also you need to work around CORS limitations. Here's a simple way to do that.

  1. First create a local certificate in /tmp if it's not there
  2. Run http-server
OptionReason
--corsEnable CORS via the Access-Control-Allow-Origin header
-SEnable https
-PProxy unhandled requests to https://tezlabapp.com
-c-1Turn off caching
-C /tmp/cert.pemCertificate file
-K /tmp/key.pemKey file

Here's something you can stick in your path to run it.

1
2
3
4
5
6
7
#!/bin/bash
if [ ! -f /tmp/key.pem ]; then
  echo Creating key
  openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout /tmp/key.pem -out /tmp/cert.pem -batch
fi

npx http-server --cors -S -P https://tezlabapp.com -c-1 -C /tmp/cert.pem -K /tmp/key.pem

Previously

mertJF/tailblocks

2020-06-09

Next

Developing React Inside Docker Clean up after your mess

2020-06-16

labnotes

Previously

IPFS and Fuse the worlds data in your filesystem

2020-06-05

Next

Upgrading emacs on debian fixing crashes

2020-06-28