Adding grunt-connect-proxy to my Yeoman generated gruntfile.js

871 views Asked by At

I'm new to Grunt (and JavaScript in general) and I'm trying to add grunt-connect-proxy to my Yeoman generated gruntfile (generator-angular 0.10.0).

I've read -->this guide to help me out but it does not seem to be done in the same way anymore.

This is what the generated gruntfile looks like

// 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 %>'
    }
  }
},

If I switch this part:

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

For this code, taken from the guide:

          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;

It will make the proxy work and I get my api from port 9000 (that my angular-app uses). But it will mess with the css. I guess it has something to do with the bower components somehow. But I don't know how to add the bower part to the tutorial code or vice versa.

Thanks alot!

1

There are 1 answers

1
Stan K. On BEST ANSWER

Here is solution (found it 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;
    }
  }
},
...