proxygruntjsgrunt-connect-proxy

Minimal example of using grunt-connect-proxy


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?


Solution

  • 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.