【问题标题】:AngularJS html5Mode using Grunt connect. grunt 0.4.5AngularJS html5Mode 使用 Grunt 连接。咕噜声 0.4.5
【发布时间】:2014-06-18 10:51:44
【问题描述】:

我最近切换到 grunt 0.4.5,它改变了连接的工作方式。

我之前使用过 connect-modrewrite,它工作得很好(在 /:parameter 生成的 url 上有一些问题)。

这是与 grunt 0.4.1 一起工作的旧版本,从 generator-angular 0.8.0 开始,中间件部分由我修改以使用 html5mode。

connect: {
    options: {
        port: 9000,
        hostname: '*IP HERE*',
        livereload: 35729,
        middleware: function (connect, options) {
            var optBase = (typeof options.base === 'string') ? [options.base] : options.base;
            return [require('connect-modrewrite')(['!(\\..+)$ / [L]'])].concat(
                optBase.map(function(path){ return connect.static(path); })
            );
        }
    },
    livereload: {
        options: {
            open: true,
            base: [
                '.tmp',
                '<%= yeoman.app %>'
            ]
        }
    },

这是来自 generator-angular 0.9.0-1 的新版本

connect: {
    options: {
        port: 9000,
        hostname: '*IP HERE*',
        livereload: 35729
    },
    livereload: {
        options: {
            open: true,
            middleware: function (connect) {
                return [
                    connect.static('.tmp'),
                    connect().use(
                        '/bower_components',
                        connect.static('./bower_components')
                    ),
                    connect.static(appConfig.app)
                ];
            }
        }
    },

我怎样才能改变它以使用 mod-rewrite 或任何其他方法来实现 html5mode?​​p>

我尝试使用这里提供的方法:https://gist.github.com/nnarhinen/7719157 我将它结合起来创建了以下内容:

middleware: function (connect) {
    return [
        connect.static(modRewrite(['^[^\\.]*$ /index.html [L]'])),
        connect.static('.tmp'),
        connect().use(
            '/bower_components',
            connect.static('./bower_components')
        ),
        connect.static(appConfig.app)
    ];
}

这允许我查看普通视图,但 modRewrite 部分似乎没有做它需要做的事情,以便通过 url 访问任何其他视图。

【问题讨论】:

  • 奇怪,我目前正在使用 grunt 0.4.5 并且中间件工作正常。我尝试了许多不同的实现,这是唯一一个使用“基本”选项的实现。

标签: angularjs gruntjs connect


【解决方案1】:

如果其他人偶然发现这个问题,这里就是解决方法:

(唯一添加的行是 modRewrite 行)

livereload: {
    options: {
        open: true,
        middleware: function (connect) {
            return [
                modRewrite(['^[^\\.]*$ /index.html [L]']),
                connect.static('.tmp'),
                connect().use(
                    '/bower_components',
                    connect.static('./bower_components')
                ),
                connect.static(appConfig.app)
            ];
        }
    }
},

确保您在 grunt 文件的顶部声明了以下内容:

var modRewrite = require('connect-modrewrite');

【讨论】:

  • 还要确保安装了 connect-modrewrite npm install connect-modrewrite --save-dev
  • 谢谢!!有很多文章解释了旧版本。我已经测试了大约 5 种不同的方法,但都没有成功。我测试了这个解决方案,效果很好。
  • @Kryx 支持替代基地怎么样?
  • @EvanPlaice 根本不知道它是如何与此交互的。
  • 我采用了这种方法,但 /:params 仍然存在问题。加载 main.css 的 url 以当前 url 而不是基本路径为前缀。就像localhost:9000/mypage/styles/main.css(加载404失败)而不是localhost:9000/styles/main.css
【解决方案2】:

鉴于其他答案非常冗长并且不保留默认的grunt-contrib-connect 中间件,我想出了使用dedicated middleware – connect-history-api-fallback 的解决方案:

npm install connect-history-api-fallback --save-dev
var history = require('connect-history-api-fallback')
//...
connect: {
    options: {
        middleware: function(connect, options, middleware) {
            middleware.unshift(history())
            return middleware
        },
        //...
    },
    //...
}

【讨论】:

    【解决方案3】:

    虽然上面接受的答案是正确的。我添加了我使用的配置,它在 CentO 上完美运行。

    以下 1 到 3 个步骤用于使用 $ grunt serve 使 Angularjs 干净的 URL 在您的本地机器上工作

    但是如果你想让它们在服务器上运行良好,特别是 nginx 你还需要更新 nginx 配置。 (第 4 步)

    1. $ npm install connect-modrewrite --save

    2. 编辑你的 gruntfile.js。在文件顶部添加

      var modRewrite = require('connect-modrewrite');
      

    然后在你的middleware:

    middleware: function (connect) {
        return [
            modRewrite(['^[^\\.]*$ /index.html [L]']),
            connect.static('.tmp'),
            connect().use('/bower_components',
            connect.static('./bower_components')),
            connect.static(config.app)
        ];
    }
    

    例如

    // Generated on 2015-11-09 using generator-angular 0.14.0
    'use strict';
    
    // # Globbing
    // for performance reasons we're only matching one level down:
    // 'test/spec/{,*/}*.js'
    // use this if you want to recursively match all subfolders:
    // 'test/spec/**/*.js'
    var modRewrite = require('connect-modrewrite');
    
    module.exports = function (grunt) {
    
      // Time how long tasks take. Can help when optimizing build times
      require('time-grunt')(grunt);
    

    3.然后去Livereload中间件,添加modRewrite

    livereload: {
        options: {
            middleware: function (connect) {
                return [
                    modRewrite([
                      '^[^\\.]*$ /index.html [L]'
                    ]),
                    connect.static('.tmp'),
                    connect().use('/bower_components', connect.static('./bower_components')),
                    connect.static(config.app)
                ];
            }
        }
    },
    

    4.编辑 NGINX 配置:

    server {
        server_name yoursite.com;
        root /usr/share/html;
        index index.html;
    
        location / {
        try_files $uri $uri/ /index.html;
        }
    }
    

    希望对你有帮助:)

    【讨论】:

      【解决方案4】:

      这是我的解决方案,适用于generator-angular 设置,但可以在任何地方使用。它允许重写语法(有趣的部分是示例 livereload configuarion)。

      connect: {
        options: {
          port: 9000,
          // Change this to '0.0.0.0' to access the server from outside.
          hostname: 'localhost',
          livereload: 35729,
          // Modrewrite rule, connect.static(path) for each path in target's base
          middleware: function (connect, options) {
            var optBase = (typeof options.base === 'string') ? [options.base] : options.base,
                middleware = [require('connect-modrewrite')(['!(\\..+)$ / [L]'])]
                  .concat(optBase.map(function (path) { 
                    if (path.indexOf('rewrite|') === -1) {
                      return connect.static(path);
                    } else {
                      path = path.replace(/\\/g, '/').split('|');
                      return  connect().use(path[1], connect.static(path[2]))
                    }
                  }));
      
            return middleware;
          }
        },
        livereload: {
          options: {
            open: true,
            base: [
              '.tmp',
              'rewrite|/bower_components|./bower_components',
              'rewrite|/app/styles|./app/styles', // for sourcemaps
              '<%= yeoman.app %>'
            ]
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-11
        • 2014-03-17
        • 2013-10-01
        • 2015-05-25
        相关资源
        最近更新 更多