labnotes

Simple CORS workaround for local development

Keep is Simple

tags
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

howto

Beginning Emacs

Rediscovering emacs

tags
emacs
javascript
floss

Next

howto

Developing React Inside Docker

Clean up after your mess

tags
javascript
docker
transient