2

私のアプリ ( ) は、テスト目的でローカル API にプロキシするhttp://localhost:8099CORS 要求を実行しています。これはで達成できますか?https://api.parse.com/http://localhost:8077grunt-connect-proxy

これは私の期待どおりに機能しない grunt-connect-proxy の設定です。

connect: {
      dev: {
        options: {
          port: 8099,
          hostname: 'localhost',
          base: 'build/',
          livereload: false,
          keepalive: true,
          middleware: function (connect, options) {
            var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
            return [
              // Include the proxy first
              proxy,
              // Serve static files.
              connect.static('build/')
            ];
          }
        }
      },
      proxies: [
      {
        context: ['api/'], //same domain api requests, proxy works fine!
        host: 'localhost',
        port: 8077
      },
      {
        context: ['api.parse.com/'], //cors, proxy is not working
        host: 'localhost',
        port: 8077,
        changeOrigin: true
      }]

    }

→ grunt serve
Proxy created for: api/ to localhost:8077
Proxy created for: api.parse.com/ to localhost:8077

したがって、基本的にプロキシはapi/リクエスト (同じドメイン) に対しては機能しますが、へのコア リクエストに対しては完全に無視されますapi.parse.com/。アイデア?

4

1 に答える 1

1

にリクエストを行うとapi.parse.com、ブラウザは実際の parse.com サーバーに接続します。grunt-connect-proxy は、あなたの場合は localhost:8099 であるアプリケーションサーバーに対してリクエストが行われた場合にのみ、画像になります。

他のすべての localhost:8099 は、アプリケーションのリモート/クロス ドメイン (localhost:8077 も含む) であり、grunt-connect-proxy を使用してサーバー側でこれらのサーバーに接続できますが、クライアント側では引き続き要求を行います。独自のサーバー。

コンテキストを使用してプロキシが構成されている場合に接続するサーバー。

proxies: [
      {
        context: ['api/'],
        host: 'localhost',
        port: 8077
      },
      {
        context: ['parse/'], 
        host: 'api.parse.com'
      }]

したがって、上記の構成を考慮すると、

localhost:8099/api--> に行きますlocalhost:8077

localhost:8099/parse--> に行きますapi.parse.com

于 2015-04-02T08:55:10.123 に答える