无论URL如何,如何告诉gulp-connect打开index.html? [英] How to tell gulp-connect to open index.html regardless of the URL?

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

问题描述

我有以下 gulp 任务:

I have the following gulp task:

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

以及以下 Angular 路线:

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).

但是,访问 /advertiser 会导致404 - 无法获取/advertiser".

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

我的主要 Angular 文件是 index.html,它可以为 / 正确打开,但不能为 /advertiser 正确打开.

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

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

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

推荐答案

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

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 ];
    }

  });
});

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

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