将grunt-connect-proxy添加到我的Yeoman生成的gruntfile.js

时间:2014-12-23 15:07:18

标签: javascript gruntjs grunt-connect-proxy

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

我已阅读-->this指南以帮助我,但似乎不会以同样的方式完成。

这就是生成的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(我的角度应用程序使用)获取我的api。 但它会搞乱css。我想这与凉亭组件有某种关系。但我不知道如何将凉亭部分添加到教程代码中,反之亦然。

非常感谢!

1 个答案:

答案 0 :(得分:4)

这是解决方案(找到它here):

...
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;
    }
  }
},
...