如何告诉gulp-connect打开index.html而不管URL?

Mis*_*hko 20 angularjs gulp

我有以下gulp任务:

gulp.task('connect', function() {
  connect.server({
    root: __dirname,
    livereload: true
  });
});
Run Code Online (Sandbox Code Playgroud)

以及以下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: "/"
    });
});
Run Code Online (Sandbox Code Playgroud)

当我访问/所有工作正常(仪表板出现).

但是,访问/advertiser结果为"404 - 无法获取/广告客户".

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

index.html无论URL 如何,我怎么能告诉Gulp打开?

Dan*_*ilo 46

或者,您可以使用为此gulp-connect调用的自己的选项fallback:

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

不需要另一个包裹.


Ove*_*ous 15

您可以使用该middleware选项来使用connect-history-api-fallback中间件.一旦安装并需要回退,就像这样添加:

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

    middleware: function(connect, opt) {
      return [ historyApiFallback ];
    }

  });
});
Run Code Online (Sandbox Code Playgroud)

  • 只是给每个发现此问题的人留言.在更新版本的`connect-history-api-fallback`中,你必须将`historyApiFallback()`称为函数.有关详细信息,请参阅[CHANGELOG](https://github.com/bripkens/connect-history-api-fallback/blob/master/CHANGELOG.md). (8认同)