1

我是 Grunt(和一般的 JavaScript)的新手,我正在尝试将 grunt-connect-proxy 添加到我的 Yeoman 生成的 gruntfile(generator-angular 0.10.0)中。

我已阅读--> 本指南以帮助我,但似乎不再以相同的方式完成。

这是生成的 gruntfile 的样子

// The actual grunt server settings
connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: 'localhost',
    livereload: 35729
  },
  proxies: [
    {
      context: '/api',
      host: 'localhost',
      port: 8080,
      https: false
    }
  ],
  livereload: {
    options: {
      open: true,
        return [
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  test: {
    options: {
      port: 9001,
      middleware: function (connect) {
        return [
          connect.static('.tmp'),
          connect.static('test'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  dist: {
    options: {
      open: true,
      base: '<%= yeoman.dist %>'
    }
  }
},

如果我切换这部分:

            return [
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];

对于此代码,取自指南:

          base: [
        '.tmp',
        '<%= yeoman.app %>'
      ],
      middleware: function (connect, options) {
        if (!Array.isArray(options.base)) {
          options.base = [options.base];
        }

        // Setup the proxy
        var middlewares = [require('grunt-connect-proxy/lib/utils').proxyRequest];

        // Serve static files.
        options.base.forEach(function (base) {
          middlewares.push(connect.static(base));
        });

        // Make directory browse-able.
        var directory = options.directory || options.base[options.base.length - 1];
        middlewares.push(connect.directory(directory));

        return middlewares;

它将使代理工作,我从端口 9000(我的 angular-app 使用)获取我的 api。但它会弄乱css。我想这与凉亭组件有关。但我不知道如何将凉亭部分添加到教程代码中,反之亦然。

非常感谢!

4

1 回答 1

4

这是解决方案(在这里找到):

...
livereload: {
  options: {
    open: true,
    //base: [
    //  '.tmp',
    //  '<%= yeoman.app %>'
    //],
    //middleware: function (connect) {
    //  return [
    //      connect.static('.tmp'),
    //      connect().use(
    //          '/bower_components',
    //          connect.static('./bower_components')
    //      ),
    //      connect.static(appConfig.app)
    //  ];
    //}
    middleware: function (connect, options) {
        var middlewares = [];

        //if (!Array.isArray(options.base)) {
        //  options.base = [options.base];
        //}

        // Setup the proxy
        middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest);

        // Serve static files
        //options.base.forEach(function(base) {
        //  middlewares.push(connect.static(base));
        //});

        middlewares.push(connect.static('.tmp'));

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

        return middlewares;
    }
  }
},
...
于 2015-01-22T16:17:10.767 回答