GULP 浏览器同步不提供 JSON 文件

gulp browser-sync not serving json files

本文关键字:JSON 文件 浏览器 同步 GULP      更新时间:2023-09-26

我遇到无法从子文件夹中提供json文件的问题。下面是代码:

var browserSync = require('browser-sync').create();
// Static server
gulp.task('connect', function() {
    browserSync.init({
      server: {
        baseDir: "app/"
      }
    });
});

我所有的staticangularjs文件都驻留在应用程序文件夹中。当我导航到http://localhost:3000时,页面加载,但app/data/myfile.json中的相应 json 文件未加载。

我在控制台中收到以下错误:

POST http://localhost:3000/data/json/myfile.json 404(未找到)

奇怪的是,当我尝试在浏览器中加载路径时,会加载 json 文件。

起初我用这种方式 https://www.browsersync.io/docs/options/#option-serveStatic 失败。但

const ASSET_EXTENSION_REGEX = new RegExp(`''b(?!''?)''.(${config.assetExtensions.join('|')})''b(?!''.)`, 'i');
     const DEFAULT_FILE = 'index.html';
...
        server: {
          baseDir: './build',
          middleware: function(req, res, next) {
            let fileHref = url.parse(req.url).href;
            if ( !ASSET_EXTENSION_REGEX.test(fileHref) ) {
              req.url = '/' + DEFAULT_FILE;
            }
            return next();
          }
        }
     ...

配置 = {资产扩展:[ 'js', 'css', "png", 'JPE?g', "动图", 'svg', "啪� "OTF", "TTC", 'TTF', 'json', "咦?" ]}

成功