2016-04-26 25 views
3

我想正確配置grunt-connect-proxy進行開發並避免發生CORS問題。grunt-connect-proxy配置不代理

我有

  • http://localhost:8080/mywebservice
  • 上的本地Tomcat上運行從在端口9000上運行的自耕農(V1.4.8)發生器它連接到上述的web服務創建的客戶端應用程序REST web服務。

我安裝了NPM模塊:

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

我Gruntfile.js配置

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', 
     (...) 
    ]); 
}); 

查詢不健全,要經過代理,因爲它仍然是原產http://localhost:9000看到。我收到以下錯誤:

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. 

雖然咕嚕服務命令打印

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

是否有印象嗎?

回答

1

該問題似乎來自您的REST服務服務器配置。 使用CORS過濾器來避免此異常。

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

實現可以根據不同您正在使用的堆棧。

我希望它可以幫助

+1

感謝您的回答。它與後端的CORS過濾器一起使用。不過,我希望這不會需要感謝changeOrigin選項。無論如何,我從0.1.11版的發行說明中看到(以及以後的版本)_0.1.11修復了節點0.10上的Websocket支持 - 將http-proxy依賴關係提升爲1.1.4,移除了不支持的http代理選項(rejectUnauthorized,timeout, changeOrigin)_ – kdefombelle