Minimal example of using grunt-connect-proxy
Asked Answered
T

1

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?

Tigre answered 31/7, 2014 at 20:21 Comment(0)
T
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.

Tigre answered 31/7, 2014 at 20:21 Comment(2)
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.Tigre
Seems that this setup no longer work with grunt 1.x because Peer [email protected] wants grunt@~0.4.1 but [email protected] wants grunt@>=0.4.0Theologize

© 2022 - 2024 — McMap. All rights reserved.