我能告诉浏览器同步总是使用一个.html文件吗? (对于html5mode链接)

我在我的gulp文件中使用浏览器同步( https://github.com/shakyShane/browser-sync )进行开发。 我想在我的angular度应用程序中使用html5mode。 对于该服务器需要将多个url模式路由到单个html文件。 分段和生产服务器已经这样做,但我也想在开发过程中启动和运行。

以下是我如何运行browser-sync服务器( gulpfile.js一部分):

 gulp.task('serve', function () { browserSync.init(null, { server: { baseDir: [APP_PATH] } }); // watch only for app specific codes; ... }); 

只是为了更清楚地说明,在我的应用程序js中,我指示angular使用html5mode进行路由:

 $locationProvider.html5Mode(true); 

在我的APP_PATH中,我有一个index.html ,当我访问browser-sync服务器时提供服务。 我需要的是browser-sync服务这个单一的文件为每个path。

所以例如,如果我试图达到/users/2path从根path开始一切都很好; 但是,如果我刷新页面或尝试直接到达/users/2browser-sync告诉它无法find正确的文档服务 – 这是很好,可以理解的,但我不知道是否有任何方式来告诉browser-sync内置服务器只能服务一个文件。 如果没有,任何人都可以build议其他选项? 我应该只运行快递服务器,并告诉browser-sync代理通过它?

你可以使用https://github.com/tinganho/connect-modrewrite

 var modRewrite = require('connect-modrewrite'); gulp.task('serve', function () { browserSync.init(null, { server: { baseDir: [APP_PATH], middleware: [ modRewrite([ '!\\.\\w+$ /index.html [L]' ]) ] } }); // watch only for app specific codes; ... }); 

https://github.com/BrowserSync/browser-sync/issues/204#issuecomment-102623643

首先安装connect-history-api-fallback

 npm --save-dev install connect-history-api-fallback 

然后将其添加到您的gulpfile.js中:

 var historyApiFallback = require('connect-history-api-fallback'); gulp.task('serve', function() { browserSync.init({ server: { baseDir: "app", middleware: [ historyApiFallback() ] } }); });