【问题标题】:How to tell gulp-connect to open index.html regardless of the URL?无论URL如何,如何告诉gulp-connect打开index.html?
【发布时间】:2014-04-11 13:02:14
【问题描述】:

我有以下 gulp 任务:

gulp.task('connect', function() {
  connect.server({
    root: __dirname,
    livereload: true
  });
});

以及以下 Angular 路线:

angular.module("MyApp", ["ngRoute"]).config(function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);

  $routeProvider
    .when("/", {
      controller: "DashboardCtrl",
      templateUrl: "templates/dashboard.html"
    })
    .when("/advertiser", {
      controller: "AdvertiserCtrl",
      templateUrl: "templates/advertiser.html"
    })
    .otherwise({
      redirectTo: "/"
    });
});

当我访问/ 时,一切正常(显示仪表板)。

但是,访问 /advertiser 会导致“404 - 无法获取 /advertiser”。

我的主要 Angular 文件是 index.html,它为 / 正确打开,但不适用于 /advertiser

我如何告诉 Gulp 打开 index.html 而不管 URL 是什么?

【问题讨论】:

    标签: angularjs gulp


    【解决方案1】:

    您也可以使用 gulp-connect 自己的选项 fallback

    gulp.task('connect', function() {
      connect.server({
        root: 'path/',
        livereload: true,
        fallback: 'path/index.html'
      });
    });
    

    不需要其他包。

    【讨论】:

    • 这就是答案!无需下载另一个包。谢谢!
    • 谢谢,这正是我想要的
    • 我已经为此寻找解决方案一两天了。谢谢,这是完美的!
    • 这实际上导致文件变成404文件,并不总是理想的。
    • 有效.. 但不要忘记用您的实际根目录替换 'path/'.. 后备需要相对于 gulp 文件的完整路径。
    【解决方案2】:

    您可以使用middleware 选项来使用the connect-history-api-fallback middleware。安装并需要后备后,您可以像这样添加它:

    gulp.task('connect', function() {
      connect.server({
        root: __dirname,
        livereload: true,
    
        middleware: function(connect, opt) {
          return [ historyApiFallback ];
        }
    
      });
    });
    

    【讨论】:

    • 非常感谢!这正是我需要的。另见:github.com/AveVlad/gulp-connect/issues/48
    • 只是给发现此问题的每个人的说明。在较新版本的connect-history-api-fallback 上,您必须将historyApiFallback() 作为函数调用。请参阅此CHANGELOG 了解更多信息。
    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 2015-11-03
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多