3

I would like to properly configure grunt-connect-proxy for development and avoid having CORS issues.

I have

  • a REST webservice running on a local tomcat at http://localhost:8080/mywebservice
  • a client application created from a yeoman (v1.4.8) generator running on port 9000. It connects to the above webservice.

I installed the npm module:

>npm install grunt-connect-proxy --save-dev
grunt-connect-proxy@0.2.0 node_modules\grunt-connect-proxy
├── lodash@0.9.2
└── http-proxy@1.11.3 (requires-port@0.0.1, eventemitter3@1.2.0)

my Gruntfile.js configuration

require('jit-grunt')(grunt, {
    (...)
    configureProxies: "grunt-connect-proxy"
});
(...)
connect: {
  options: {
    port: 9000,
    hostname: 'localhost',
    livereload: 35729
  },
  proxies: [
     {
       context: ['/mywebservice'],
       host: 'localhost',
       port: 8080,
       changeOrigin: true
     }
   ],
  livereload: {
    options: {
      open: true,
      middleware: function (connect) {
        return [
          require('grunt-connect-proxy/lib/utils').proxyRequest,
          connect.static('.tmp'),
          (...)
          connect.static(appConfig.app)
        ];
      }
    }
  },
(...)
}

grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
(...)
    grunt.task.run([
      (...)
      'configureProxies:server',
      (...)
    ]);
});

The query does not sound to go through the proxy as it is still seen with origin http://localhost:9000. I received the following error:

XMLHttpRequest cannot load http://localhost:8080/xmlcompare-rs/xmlcompare. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

although grunt serve command prints

Running "configureProxies:server" (configureProxies) task
Proxy created for: /mywebservice to localhost:8080

Does it ring a bell?

1 Answers1

1

The issue seems to come from your REST services server configuration. Use a CORS Filter to avoid this exception.

https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter

The implementation may differs depending on the stack you are using.

I hope it may helps

Tony
  • 482
  • 4
  • 13
  • 1
    Thanks for your answer. It works with a CORS Filter on the back end. However I expected it will not be needed thanks to the changeOrigin option. Anyway I see from the release notes of version 0.1.11 (and so in onwards version) _0.1.11 Fix Websocket support on Node 0.10 - Bumped http-proxy dependency to 1.1.4, Removed unsupported http-proxy options (rejectUnauthorized, timeout, changeOrigin)_ – kdefombelle Apr 26 '16 at 10:23