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?

4

1 回答 1

1

问题似乎来自您的 REST 服务服务器配置。使用 CORS 过滤器来避免此异常。

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

根据您使用的堆栈,实现可能会有所不同。

我希望它可以帮助

于 2016-04-26T10:10:20.117 回答