Skip to content

A simple CORS proxy server with configurable allowed domains

License

Notifications You must be signed in to change notification settings

BeyondCodeBootcamp/cors-server.js

 
 

Repository files navigation

cors-server.js

A simple CORS proxy server with configurable allowed domains.

QuickStart

  1. Install node
    curl https://webi.sh/node | sh
    source ~/.config/envman/PATH.env
  2. Download and enter the project directory
    git clone https://github.com/dashhive/cors-server.js
    pushd ./cors-server.js/
  3. Run cors-server with config options
    # ./bin/cors-server.js [flags]
    ./bin/cors-server.js --prefix /api/cors/ --port 2677 --domains 'example.com, example.org'
  4. Test with curl
    curl http://localhost:2677/api/cors/example.com
    (browser testing requires https, see below)

Usage

Configuration

You can either set command line flags, or set ENVs. Flags take precedence.

flag ENV example description
--port PORT= 2677 Number
--prefix CORS_PROXY_PREFIX= /api/cors/ URL Path
--domains CORS_ALLOWED_DOMAINS= 'example.com, example.org' URL Hostname

See ./example.env for config options.

Set HTTPS for localhost with Caddy

  1. Install caddy
    curl https://webi.sh/caddy | sh
    source ~/.config/envman/PATH.env
  2. Create ./Caddyfile
    https://localhost:443 {
        handle /api/* {
            reverse_proxy localhost:2677
        }
    }
  3. Run caddy and localhost https will be generated
    caddy run --config ./Caddyfile

Test in a browser console

  1. Open the JavaScript console
    Cmd ⌘ + Alt ⌥ + I
  2. Test it out with fetch
    await fetch("https://localhost/api/cors/example.com/", { mode: "cors" });

Run as a Service

  1. Install Serviceman
    curl https://webi.sh/serviceman | sh
    source ~/.config/envman/PATH.env
  2. Generate and enable service file for cors-server
    sudo env PATH="$PATH" \
        serviceman add --system -- \
            ./bin/cors-server.js --
                --port 2677 \
                --prefix /api/cors/ \
                --domains example.com
  3. Generate and enable service file for caddy
    sudo env PATH="$PATH" \
        serviceman add --system --cap-net-bind -- \
        caddy run --config ./Caddyfile

About

A simple CORS proxy server with configurable allowed domains

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 97.2%
  • Shell 2.8%