Question

I have been working all day on trying to get my proxies set up in my Gruntfile. Here is my Gruntfile:

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

module.exports = function(grunt) {

  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    connect:{
      livereload: {
        options: {
          middleware: function (connect) {
            return [proxySnippet];
          }
        }
      },
      options: {
        port: 9000,
        base: 'app',
        keepalive: true,
        livereload: true
      },
      proxies: [
        {
          context: '/public/api',
          host: 'localhost',
          port: 8182,
          https: false,
          rewrite: {
            '^/public/api': ''
          }
        }
      ]
    }
  });

  grunt.registerTask('server', ['less', 'configureProxies', 'connect', 'connect', 'watch', 'open:dev']);
};

When I run my grunt server I can only hit my proxy. If I try to just hit anything other than the proxy I get a 404. What is giving me this issue?

Était-ce utile?

La solution

I also had a lot of trouble setting up a proxy using grunt-connect-proxy.

Digging in the source code of grunt-contrib-connect, I realized that it uses the nodeJs Connect framework behind the scene.

Internally the middleware option defaults to this function:

function (connect, options) {
    var middlewares = [];
    if (!Array.isArray(options.base)) {
        options.base = [options.base];
    }
    var directory = options.directory || options.base[options.base.length - 1];
    options.base.forEach(function (base) {
        // Serve static files.
        middlewares.push(connect.static(base));
    });
    // Make directory browse-able.
    middlewares.push(connect.directory(directory));
    return middlewares;
}

Which basically adds the connect.static and the connect.directory middlewares to an array passed to the connect(middlewares) constructor.

Knowing that, we can make use of the proxy-middleware nodeJs package like this:

connect: {
    server: {
        options: {
            port: 9002,
            keepalive: true,
            middleware: function (connect, options) {
                // Proxy all requests to target the local application.
                var proxyOptions = require('url').parse('http://localhost:8080/');
                proxyOptions.route = '/api';
                return [
                    require('proxy-middleware')(proxyOptions), // Include the proxy first.
                    connect.static(options.base), // Serve static files.
                    connect.directory(options.base) // Make empty directories browse-able.
                ];
            }
        }
    }
}

Basically we are adding a middleware to the middleware array. This new proxy middleware will translate any incoming request like http://localhost:9002/api/ into http://localhost:8080/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top