0

我正在使用 yeoman generator-angular 应用程序和远程 API,我的 gruntfile 配置似乎很好:

// Generated on 2015-06-01 using generator-angular 0.11.1
'use strict';
module.exports = function (grunt) {
  / * Removed for brevity */
  grunt.initConfig({
    yeoman: appConfig,
    watch: {
      / * Removed for brevity */
    },
    connect: {
      options: {
        port: 9000,
        hostname: 'localhost',
        livereload: 35729
      },
      proxies: [
        {
          context: '/api',
          host: 'subdomain.hostname.com',
          xforward: true,
          secure: false,
          headers: {
            'Authorization': 'RemovedToken'
          },
        }
      ],
      livereload: {
        options: {
          open: true,
          middleware: function (connect, options) {
            var middlewares = [require('grunt-connect-proxy/lib/utils').proxyRequest];

            middlewares.push(
                connect.static('.tmp'),
                connect().use('/bower_components',connect.static('./bower_components')),
                connect().use('/app/styles', connect.static('./app/styles')),
                connect().use(connect.static(appConfig.app))
            );

            return middlewares;
          }
        }
      },
      / * Removed for brevity */
    },
    / * Removed for brevity */
  });

  grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
      'clean:server',
      'wiredep',
      'concurrent:server',
      'autoprefixer:server',
      'configureProxies:server',
      'connect:livereload',
      'watch'
    ]);
  });

  grunt.loadNpmTasks('grunt-connect-proxy');
};

grunt serve --verbose 在发出请求时返回:

Proxied request: /api/foo/bar/?baz=6 -> http://subdomain.hostname.com:80/api/foo/bar/?baz=6
{
  "host": "localhost:9000",
  "connection": "keep-alive",
  "accept": "application/json, text/plain, */*",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36",
  "referer": "http://localhost:9000/",
  "accept-encoding": "gzip, deflate, sdch",
  "accept-language": "en-US,en;q=0.8,es;q=0.6",
  "cookie": "connect.sid=s%3AQpOp8pdI8GM2g8s0qCX602-ds-Xs-5le.C%2BrosIxHQ4ZcLLPgZKhABjji18kqK2K73C7mjF6scqM",
  "Authorization": "RemovedToken",
  "x-forwarded-for": "127.0.0.1",
  "x-forwarded-port": "9000",
  "x-forwarded-proto": "http"
}

我在那里找不到任何错误,也许“主机”仍然是 localhost:9000,但我不确定这是否是个问题,在 Chrome 中我得到:503 Service Unavailable,但确实有服务正在运行,我知道,因为我用 Postman 对其进行了测试,一切正常,有人可以帮助我吗?也许我在配置中遗漏了一些东西?

谢谢。

4

1 回答 1

0

在项目中挖掘了更多内容后,我发现我也必须指定“主机”标头。

https://github.com/drewzboto/grunt-connect-proxy/issues/72

于 2015-06-15T18:53:04.260 回答