I have an Angular SPA Javascript app that needs to use various REST API's. This post discussing setting up a proxy server to address browser same origin issues using Steve Jansen's nifty json-proxy package. I'm doing this on a Raspberry Pi.
Installing json-proxy
I'm going to assume you have node.js install and npm. Install json-proxy:
npm install -g json-proxy
Before customizing the configuration, let's first make sure that one of Steve's sample configurations works. I'm going to use the config file available here and the index.html that accompanies it. Here's what the config file looks like:
{ "server": { "port": 8080, "webroot": "$config_dir", "html5mode": "/index.html" }, "proxy": { "forward": { "/channel": "https://www.youtube.com:443", "/events/(.*)": "http://news.google.com/news/section?topic=$1", "/news/": "http://news.google.com" }, "headers": { "X-Remote-User": "John Doe" } } }
Put the index.html and json config file in a directory of your choosing and start the proxy with the command:
json-proxy -c json-proxy.json
You should now be able to browser to your local server at port 8080, view the index.html and try out the proxy to youtube and Google news.
To setup our proxy to run on system boot, first create the file /etc/init.d/json-proxy with contents something like this:
#!/bin/sh case "$1" in start) echo "Starting json-proxy" /usr/local/bin/json-proxy -c /home/pi/sdnet.link/proxy.json ;; stop) echo "Killing json-proxy" killall -9 json-proxy ;; restart) echo "Killing json-proxy" killall -9 json-proxy echo "Starting json-proxy" /usr/local/bin/json-proxy -c /home/pi/sdnet.link/proxy.json ;; force-reload) ;; *) echo "Usage: $0 start" >&2 exit 3 ;; esac
Then enter the command to create the rc.d files:
sudo update-rc.d json-proxy default