【问题标题】:Weird... Angular Proxy not working properly奇怪... Angular 代理无法正常工作
【发布时间】:2015-02-18 20:26:57
【问题描述】:

我正在开发一个 Angular / Laravel 项目,但我在配置 Grunt 代理时遇到了麻烦正确地,我已经遵循了几个教程并进行了一些研究以解决这个问题,但我没有成功。

问题是配置的代理没有按照应有的方式重定向到主机,我设置将请求从 /localhost:9000/api 转发到 /localhost:8000/api ,但是当我发出请求时:

推荐网址:http://localhost:9000/api/whataver
请求网址:http://localhost:9000/api/whatever

正在向同一主机发出请求,因此请求没有正确代理,有什么建议吗?

这是我的 grunt 文件的一部分:

connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: '127.0.0.1',
    livereload: 35729
  },
  proxies: [
      {
          context: '/api',
          host: '127.0.0.1',
          port: 8000,
          https: false,
          changeOrigin: false
      }
  ],
  livereload: {
    options: {
      open: true,
      base: [
       '.tmp',
       '<%= yeoman.app %>'
      ],
      middleware: function (connect,options) {

        var middlewares = [];
        
        if (!Array.isArray(options.base)) {
          options.base = [options.base];
        }

        // Set up the proxy
        middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest);

        //server static files
        middlewares.push(
          modRewrite(['^[^\\.]*$ /index.html [L]']),
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
             connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        );
        return middlewares;
      }
    }
  }

感谢您的帮助,谢谢!

【问题讨论】:

  • 嘿,赫克托,我在这里遇到了完全相同的问题,您找到解决方案了吗?谢谢!
  • 您好 danboh,是的,我找到了解决方案...让我知道您的做法,也许我可以帮助您...不过我会发布解决方案我发现我的实际配置。问候

标签: angularjs gruntjs yeoman yeoman-generator-angular


【解决方案1】:

这个答案应该会有所帮助

AngularJS with Grunt - Connect to another server

严格按照回答中提到的步骤检查您的代码。 你会得到它。

【讨论】:

    【解决方案2】:

    很久没问这个问题了。我当时找到了解决方案,但我从未发布过我的解决方案,所以就这样吧:

    首先,配置没有任何问题,我的 Laravel 和 Angular 都一切正常。显然问题是 Laravel 代理的错误:

    当你使用 artisan 命令为你的 Laravel 应用提供服务时:

    php artisan serve 
    

    默认在localhost:8000服务,没错

    当您使用 grunt 服务您的 Angular 应用程序时:

    grunt serve
    

    默认情况下它在 localhost:9000 中提供服务,它也可以,但是由于两个服务器都使用逻辑 localhost,那么这会导致 Laravel 应用程序中的一些错误。

    它可以像在同一个本地主机中提供服务一样简单,但换句话说,就是字面意思。这是解决方案:

    php artisan serve --host 127.0.0.1
    

    就是这样,这个错误现在必须消失。

    注意:此解决方案仅适用于本地环境,我不知道它在生产中的表现如何。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2021-09-15
      • 2017-10-12
      • 2015-12-26
      • 2019-10-17
      相关资源
      最近更新 更多