AngularJS html5Mode使用Grunt connect,咕噜声0.4.5

bfrts1fy  于 2023-08-02  发布在  Angular
关注(0)|答案(4)|浏览(103)

我最近切换到grunt 0.4.5,它改变了connect的工作方式。
我以前使用connect-modrewrite,它工作得很好(有一些/:parameter生成的URL的问题)。
下面是使用generator-angular 0.8.0的grunt 0.4.1的旧版本,中间件部分由我修改为使用html5 mode。

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或任何其他方法来实现html5 mode?
我尝试使用这里提供的方法: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到达任何其他视图。

oaxa6hgo

oaxa6hgo1#

如果其他人偶然发现这是修复:
(the添加的唯一行是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');

ghhkc1vu

ghhkc1vu2#

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

npm install connect-history-api-fallback --save-dev

个字符

wkftcu5l

wkftcu5l3#

虽然上面的答案是正确的。我添加了我使用的配置,它在CentOs上完美地工作。
下面的1到3个步骤是使用$ grunt serve使Angularjs干净的URL在本地机器上工作
但是如果你想让它们在服务器上正常运行,特别是nginx,你还需要更新nginx配置。(步骤4)

  1. $ npm install connect-modrewrite --save
    1.编辑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.然后转到Liverload中间件,添加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;
    }
}


希望有帮助:)

s4n0splo

s4n0splo4#

下面是我的解决方案,适用于generator-angular设置,但可以在任何地方使用。它允许重写语法(有趣的部分是livererload 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 %>'
      ]
    }
  }
}

字符串

相关问题