kdefombelle
kdefombelle

Reputation: 63

grunt-connect-proxy configuration does not proxy

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

I have

I installed the npm module:

>npm install grunt-connect-proxy --save-dev
[email protected] node_modules\grunt-connect-proxy
├── [email protected]
└── [email protected] ([email protected], [email protected])

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?

Upvotes: 3

Views: 941

Answers (1)

Tony
Tony

Reputation: 482

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

Upvotes: 1

Related Questions