Simple CORS workaround for local development

Published June 15, 2020 #howto, #javascript, #cors, #static_sites

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

--corsEnable CORS via the Access-Control-Allow-Origin header
-SEnable https
-PProxy unhandled requests to
-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.

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

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

Read next

See also

Beginning Emacs

Rediscovering emacs

Intro I've been dipping back ito the world of free software, and have been having a huge amount of fun getting back into emacs. I'm using it more of an operating system than I ever did before, as a way to navigate the world using a text interface. I'd never thought I'd be done with the terminal, but this is the first time that I've got something better and more powerful.

Read more

Making charts with VueJS and no tooling

Static files all the way

I learned most of what I know about coding by looking at source code – especially view source on a web browser. Lets see if we can bring that era back a bit by using ES modules and eschewing webpack and other bundling systems. We will use the amazing CDN to get our building blocks to assemble together. First get vue working From unpkg we will link to tailwindcss in the head tag, and then import vuejs from using a script type="module" tag.

Read more

Setting up an IPFS Node

using docker-compose and certbot

IPFS nodes that run in the broswer communicate over websockets to the main network. Lets walk through how to setup a IPFS server that your browser code can connect to in addition to the public gateways. Strategy Wire everything up with docker-compose Create and configure an ipfs container Setup nginx with dummy certificate Replace that certificate with certbot Setup certbot to auto renew the certificates Requirements You will need: A server with a domain or subdomain to use certbot to get a certificate A working docker-compose install I'm using a prebuilt docker image on Digital Ocean but the key part is to get the domain name.

Read more