如何告知gulp-connect打开index.html而不管网址如何? [英] How to tell gulp-connect to open index.html regardless of the URL?

查看:117
本文介绍了如何告知gulp-connect打开index.html而不管网址如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以及以下Angular路线:



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

当我访问 / 时,但是,访问 / advertiser 会导致404 - 无法获得/广告客户。 / p>

我的主要Angular文件是 index.html ,可以正确打开 / code>,但对于 / advertiser



我怎么能告诉Gulp打开 index.html 不管URL是什么?

解决方案

您可以使用 middleware 选项可使用 connect-history-api -fallback 中间件。一旦你已经安装并要求回退,你可以像这样添加它:

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

中间件:函数(connect,opt){
return [ historyApiFallback];
}

});
});


I have the following gulp task:

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

and the following Angular routes:

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: "/"
    });
});

When I visit / all works fine (the Dashboard appears).

But, visiting /advertiser results in "404 - Cannot GET /advertiser".

My main Angular file is index.html, which is opened properly for /, but not for /advertiser.

How could I tell Gulp to open index.html regardless of the URL?

解决方案

You can use the middleware option to use the connect-history-api-fallback middleware. Once you've installed and required the fallback, you add it like this:

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

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

  });
});

这篇关于如何告知gulp-connect打开index.html而不管网址如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆