javascriptyeoman-generator-angulargrunt-connect-proxy

Adding grunt-connect-proxy to generator-angular gruntfile.js


I'm trying to add grunt-connect-proxy to my gruntfile.js in a yeoman generator-angular project (generator-angular 0.15.1) but I can't seem to get it to work since the way it's written changes and I'm inexperienced in how Grunt works.

I've read many posts about this and none are particularly up-to-date, and the gruntfile changes seemingly often in how it implements livereload middleware This makes the documentation for grunt-connect-proxy to not work in my case.

The tricky part is under livereload

This is how it looks in generator-angular 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,
      xforward: false
    }],
  livereload: {
    options: {
      open: true,

      // --- how the code looks like before I do anything

      middleware: function (connect) {
        return [
          connect.static('.tmp'),
          connect().use('/bower_components', connect.static('./bower_components')),
          connect().use('/app/styles', connect.static('./app/styles')),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  ...

When I look at the documentation it looks like this:

    livereload: {
        options: {
            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;
            }
        }
    }

Can someone help me translate the documentation to the new way of writing the middleware part?

Thanks!!


Solution

  • So I got some help and this is how it was solved:

      livereload: {
        options: {
          open: true,
          middleware: function(connect) {
            var middlewares = [require('grunt-connect-proxy/lib/utils').proxyRequest];
            return middlewares.concat(
              connect.static('.tmp'),
              connect().use('/bower_components', connect.static('./bower_components')),
              connect().use('/app/styles', connect.static('./app/styles')),
              connect.static(appConfig.app)
            );
          }
        }
      }
    

    Hope this helps someone else too.