forwarding grunt connect to different url

你。 提交于 2020-01-05 08:00:51

问题


I'm using grunt connect with livereload for my dev environment.

I want to be able to call the production api which is under /server.

To do that I need to direct any calls from

http://localhost:9000/server

to

http://www.production-server.com/server

This is good for me because sometimes I want to test against the production server when in dev mode.

Here's my current connect configuration (Generated by Yeoman):

connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: 'localhost',
    livereload: 35729
  },
  livereload: {
    options: {
      open: true,
      middleware: function(connect, options, middlewares) {
        return [
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  test: {
    options: {
      port: 9001,
      middleware: function(connect) {
        return [
          connect.static('.tmp'),
          connect.static('test'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  dist: {
    options: {
      open: true,
      base: '<%= yeoman.dist %>'
    }
  }
},

回答1:


I've found the problem and the solution:

First thing to do is to use the: grunt-connect-proxy grunt task to be able to do proxy (You can do really anything there). The configuration is not obvious, but you can find all the info (and example) here: https://www.npmjs.org/package/grunt-connect-proxy

My specific problem was because my server did not accept any calls that did not come from the same domain, so all I did was to add "headers" property to the config with my domain name. this is how the new config should look like:

    connect: {
        options: {
            port: 9000,
            // Change this to '0.0.0.0' to access the server from outside.
            hostname: 'localhost',
            livereload: 35729
        },
        server: {
            proxies: [
                {
                    context: '/server',
                    host: 'production-server.com',
                    post: 80,
                    changeOrigin: true,
                    headers: {
                        host: 'simple-layout.com'
                    }
                }
            ]
        },
        livereload: {
            options: {
                open: true,
                middleware: function (connect) {
                    return [
                        proxySnippet,
                        connect.static('.tmp'),
                        connect().use(
                            '/bower_components',
                            connect.static('./bower_components')
                        ),
                        connect.static(appConfig.app)
                    ];
                }
            }
        },
        test: {
            options: {
                port: 9001,
                middleware: function (connect) {
                    return [
                        connect.static('.tmp'),
                        connect.static('test'),
                        connect().use(
                            '/bower_components',
                            connect.static('./bower_components')
                        ),
                        connect.static(appConfig.app)
                    ];
                }
            }
        },
        dist: {
            options: {
                open: true,
                base: '<%= yeoman.dist %>'
            }
        }
    },


来源:https://stackoverflow.com/questions/26721650/forwarding-grunt-connect-to-different-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!