Friday, September 15, 2017

Proxy Api Calls in Vue2

The big advantage is we don't have to worry about CORS

npm install --save vue-resource
npm install --save-dev http-proxy-middleware

in build/index.js add
'/service': {
 target: '',
 changeOrigin: true, 
 ws: true,   // proxy websockets
 pathRewrite: {
   '^/service/state/': '/state/get/'   // http://localhost:8080/service/state/ =>
 router: {

If it is configured properly, you will see this when the node server is started.
[HPM] Proxy created: /service  ->
[HPM] Proxy rewrite rule created: "^/service/state/" ~> "/state/get/"

in .js file
// send request to http://localhost:8080/service/state/USA/all will be eventually sent to
this.$http.get('/service/state/USA/all', {})  
        .then(response => {
            this.states = response.body.RestResponse.result
        }, response => {

No comments:

Post a Comment