drewzboto / grunt-connect-proxy

Grunt Connect support for proxying API calls during development
MIT License
424 stars 122 forks source link

Proxy broken? #63

Open JSteunou opened 10 years ago

JSteunou commented 10 years ago

I tried with a simple gruntfile but never succeed to make it works

/* global module */

module.exports = function(grunt) {

    grunt.initConfig({

        connect: {
            server: {
                options: {
                    hostname: 'localhost',
                    open: true,
                    port: 8080,
                    keepalive: true

                },
                proxies: [
                    {
                        context: '/api',
                        host: 'localhost',
                        port: 8001
                    }
                ]
            }
        }

    });

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

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

};

Opening localhost:8080/api I have a Cannot GET /api displayed even if I create a folder api in the folder served under my other server on 8001.

If I create a folder api under the folder of this Gruntfile I have no longer the error... but that's not what I expected.

bekzod commented 10 years ago

+1

bekzod commented 10 years ago

PLESE HELP

JSteunou commented 10 years ago

If you need serious help go directly with nginx proxy pass.

bekzod commented 10 years ago

I am using nginx already, just wanted to switch to grunt and grunt-connect-proxy

JSteunou commented 10 years ago

I made a nodejs proxy directly with node http proxy from nodejitsu, you could do the same and create a grunt task from it.

bekzod commented 10 years ago

cool thanks

oh4real commented 10 years ago

I ran into similar problem. grunt serve for an Angular app was running on localhost:9000 and I needed to proxy API requests to a Laravel PHP app running on localhost:8000.

Logging from PHP app started with php -S localhost:8000 logs showed "Invalid request (Unexpected EOF)" while I could access localhost:8000 just fine.

Apparently something weird was happening when proxying localhost:9000 to localhost:8000 and I "solved" it by telling PHP app to start up as 127.0.0.1:8000.

Gruntfile.js

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: 8000, ...

Angular client side app with api data proxy

my-machine$ grunt serve ... Running "configureProxies:server" (configureProxies) task Proxy created for: /_/api to localhost:8000 ... Started connect web server on 127.0.0.1:9000. ... Waiting...OK

And on php side:

php -S 127.0.0.1:8000 PHP 5.4.28 Development Server started at Tue Jun 10 23:03:51 2014 Listening on http://127.0.0.1:8000

This solved my problems. Seemed a bit weird, but whatevs.

JSteunou commented 10 years ago

I'll give it a try with 127.0.0.1 instead of localhost

vampolo commented 10 years ago

Same problem here with this configuration:

            server2: {
                options: {
                    port: 9000,
                },
                proxies: [
                    {
                        context: '/cortex',
                        host: '10.10.2.202',
                        port: 8080,
                        https: false
                    },{
                        context: '/google',
                        host: 'google.com'
                    },{
                        context: '/test',
                        host: 'localhost',
                        port: 12345
                    }]
            }
        grunt.task.run([
            'configureProxies:server2',
            'connect:server2',
            'open',
            'watch'
        ]);

on the terminali i get:

➜  frontend git:(advanced-testing) ✗ grunt remote
Running "remote" task

Running "configureProxies:server2" (configureProxies) task
Proxy created for: /cortex to 10.10.2.202:8080
Proxy created for: /google to google.com:80
Proxy created for: /test to localhost:12345

Running "connect:server2" (connect) task
Started connect web server on http://0.0.0.0:9000

Running "open:server" (open) task

Running "watch" task
Waiting...

Of corse none of the proxies work and i get Cannot GET /[PATH HERE] for all the configured proxies.

vampolo commented 10 years ago

I got it working. Poor documentation is to blame in this case. To get it working u must change the middleware property as said in the (documentation)[https://github.com/drewzboto/grunt-connect-proxy#adding-the-middleware]

JSteunou commented 10 years ago

@vampolo Thank you, I will give it a try next time. Weird the middleware is not added automatically.

sidwood commented 9 years ago

Like others, I've had trouble getting this plugin to work. It's so much easier to write your own.

Here's one for my current project. Hopefully this will give others a good starting place to work from.

// tasks/server.js
'use strict';

var connect = require('connect');
var httpProxy = require('http-proxy');
var serveStatic = require('serve-static');

var server = connect();
var proxy = httpProxy.createProxyServer({target: 'http://localhost:3000'});
var latentProxyMiddleware = function(req, res) {
  setTimeout(function() {
    proxy.web(req, res);
  }, 500);
};

module.exports = function(grunt) {
  grunt.registerTask('server:build', function() {
    this.async(); // run forever
    server.use(serveStatic(grunt.config('paths.build')));
    server.use('/', latentProxyMiddleware);
    server.listen(8000);
  });

  grunt.registerTask('server:dev', function() {
    this.async(); // run forever
    server.use(serveStatic(grunt.config('paths.scratch')));
    server.use(serveStatic(grunt.config('paths.bower')));
    server.use(serveStatic(grunt.config('paths.app')));
    server.use('/', latentProxyMiddleware);
    server.listen(8000);
  });
};
realdah commented 9 years ago

Initially, it does not work for me, however, it is more likely a combination of configuration game to do this magic. Below document is so good and simple to save multiple hours for me!

http://www.hierax.org/2014/01/grunt-proxy-setup-for-yeoman.html

While, still you need to be aware that, in middleware configuration, you need to combine the static content with the proxy settings recommended by its document.

then, we will still use same sever to view the angular part, but we can proxy the rest api part to any places we want, either local or remote.

    // 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: 8008
      }],
      livereload: {
        options: {
          open: true,
          middleware: function (connect, options) {
            var middlewares = [];

            middlewares.push(connect.static('.tmp'));
            middlewares.push(connect().use(
              '/bower_components',
              connect.static('./bower_components')
            ));
            middlewares.push(connect.static(appConfig.app));

            // 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));
            });

            return middlewares;
          }
        }
      }
}
scirelli commented 7 years ago

I had forgotten to do this which is why wasn't working.

    grunt.registerTask('server', function (target) {
        grunt.task.run([
            'clean:server',
            'compass:server',
            'configureProxies:server',  //This
            'livereload-start',
            'connect:livereload',
            'open',
            'watch'
        ]);
    });