转发咕噜连接到不同的网址 [英] forwarding grunt connect to different url

查看:194
本文介绍了转发咕噜连接到不同的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望能够调用位于/server.

要做到这一点,我需要从


http:// localhost:9000 / server



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


这对我有好处,因为有时我想在开发模式下测试生产服务器。



这是我目前的连接配置(由Yeoman生成):

connect:{options:{port:9000,//将其更改为'0.0.0.0'以访问外面的服务器。主机名: '本地主机',livereload:35729},livereload:{选择:{开:真,中间件:功能(连接,期权,中间件){返回[connect.static( 'TMP'。),连接()使用(。 '/ 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 bower_components',connect.static('./ bower_components')),connect.static(appConfig.app)]; }}} dist:{options:{open:true,base:'<%= yeoman.dist%>'}}},


$ b $我发现了这个问题和解决方案:

首先要做的是使用:grunt-connect-proxy grunt任务来完成代理(你可以在那里做任何事情)。配置不明显,但您可以在此处找到所有信息(和示例): https://www.npmjs .org / package / grunt-connect-proxy



我的具体问题是因为我的服务器不接受来自同一个域的任何调用,所以我所做的只是添加标题属性到我的域名配置。这是新的配置应该是这样的:

  connect:{
options:{
port: 9000,
//将其更改为'0.0.0.0'以从外部访问服务器。
主机名:'localhost',
livereload:35729
},
服务器:{
代理:[
{
context:'/服务器',
主机:'production-server.com',
发布:80,
changeOrigin:true,
标题:{
主机:'simple-layout .com'
}
}
]
},
livereload:{
options:{
open:true,
中间件:函数(连接){
返回[
proxySnippet,
connect.static('。tmp'),
connect()。use(
'/ bower_components ',
connect.static('./ bower_components')
),
connect.static(appConfig.app)
];


$ b 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:{
选项:{
open:true,
base:'<%= yeoman .dist%>'
}
}
},


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 %>'
    }
  }
},

解决方案

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 %>'
            }
        }
    },

这篇关于转发咕噜连接到不同的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆