【问题标题】:browserSync's modRewrite does not work for angularjs html5mode for path 'app/index.html' with gulpbrowserSync 的 modRewrite 不适用于 angularjs html5mode for path 'app/index.html' with gulp
【发布时间】:2014-12-17 02:13:58
【问题描述】:

当我运行 gulp 时,服务器以消息启动:无法获取 /。 当我指向 localhost:3000/app/index.html 时,站点重定向到 localhost:3000/home 并正常工作。 但是,当我重新加载页面时,它给出:无法获取 /home。

请查看以下配置,看看是否有遗漏:

访问路径:app/index.html

这是我的 gulpfile.js:

var gulp = require('gulp'),
nodemon = require('gulp-nodemon'),
jshint = require('gulp-jshint'),
browserSync = require('browser-sync')
modRewrite  = require('connect-modrewrite');

gulp.task('lint', function () {
  gulp.src('app/js/*.js').pipe(jshint());
});

gulp.task('serve', function() {
  browserSync({
      server: {
          baseDir: "./",
          middleware: [
              modRewrite([
                  '!\\.\\w+$ /index.html [L]'
              ])
          ]
      }
  });
});

gulp.task('default', ['lint', 'serve'], function() {
  gulp.watch('app/js/*.js', ['lint', browserSync.reload]);
});

角度路径文件:

  $urlRouterProvider.otherwise("/home");

  $stateProvider
    .state('home', {
      url: "/home",
      templateUrl: "app/partials/home.html",
      controller: 'HomeCtrl'
    })
...

非常感谢!

Github:https://github.com/yhjor1212/angular-fire-powder

【问题讨论】:

    标签: angularjs node.js mod-rewrite gulp browser-sync


    【解决方案1】:

    在 modRewrite 中使用这段代码:

    ['^([^.]+)$ /index.html [L]']

    例子:

    gulp.task('serve', function() {
      browserSync({
          server: {
              baseDir: "./",
              middleware: [
                  modRewrite(['^([^.]+)$ /index.html [L]'])
              ]
          }
      });
    });
    

    【讨论】:

      【解决方案2】:

      抱歉,我不了解 gulp,但使用 Gruntfile.js 我用 modRewrite 模块修复了中间件部分,如下所示:

      var modRewrite = require('connect-modrewrite');
      
      ...
      
      grunt.initConfig({
          ...
          browserSync: {
              ...
              options: {
                  server: {
                      middleware: [
                          modRewrite(['!\.html|\.js|\.css|\.png$ /index.html [L]'])
                      ]
                  }
              }
      ...
      

      完美运行!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多