13

I have an angularJs App which I built with grunt and a server backend written in Java running on a tomcat server. To wire those together when development I wanted to use grunt-connect-proxy. But I could not get it to work even a bit.

All the "examples" and "demos" I found on the web happened to use a several hundred lines long Gruntfile.js. That turned out not to be really useful in finding my problem. What does a minimal (!) example look like?

mwilcox
  • 4,065
  • 23
  • 20
yankee
  • 38,872
  • 15
  • 103
  • 162

1 Answers1

30

This is how you can create a minimal demo which is just a proxy to google.com:

Run:

npm install grunt-connect-proxy --save-dev
npm install grunt-contrib-connect --save-dev

and creat the following Gruntfile.js:

module.exports = function (grunt) {

    var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;

    grunt.initConfig({
        connect: {
            server: {
                options: {
                    hostname: 'localhost',
                    keepalive: true,
                    open: true,
                    middleware: function (connect, options) {
                        return [proxySnippet];
                    }
                },
                proxies: [{
                    context: '/',
                    host: 'google.com',
                    port: 80
                }]
            }
        }
    });

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

    grunt.registerTask('default', [
        'configureProxies:server',
        'connect:server']);

};

Now just run grunt.

yankee
  • 38,872
  • 15
  • 103
  • 162
  • 1
    In my case I was missing the `:server` behind `configureProxies`. I noticed after more and more cross checking shortly before posting this code as question. – yankee Jul 31 '14 at 20:24
  • 2
    Seems that this setup no longer work with `grunt 1.x` because `Peer grunt-connect-proxy@0.2.0 wants grunt@~0.4.1` but `grunt-contrib-connect@1.0.2 wants grunt@>=0.4.0` – Gian Marco Aug 11 '16 at 12:59