简体   繁体   English

gulp browser-sync不提供json文件

[英]gulp browser-sync not serving json files

I have an issue where i am not able to serve json files from my sub-folders. 我有一个问题,我无法从我的子文件夹中提供json文件。 Below is the code: 以下是代码:

var browserSync = require('browser-sync').create();

// Static server
gulp.task('connect', function() {
    browserSync.init({
      server: {
        baseDir: "app/"
      }
    });
});

All my static and angularjs files reside inside app folder. 我的所有staticangularjs文件都驻留在app文件夹中。 When i navigate to http://localhost:3000 , the page loads but the corresponding json file inside app/data/myfile.json does not load. 当我导航到http://localhost:3000 ,页面加载但app/data/myfile.json中的相应json文件无法加载。

I get the below error in my console: 我在控制台中收到以下错误:

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

The strange thing is when i try to load the path in my browser, the json file loads. 奇怪的是当我尝试在浏览器中加载路径时,json文件会加载。

At first I used this way https://www.browsersync.io/docs/options/#option-serveStatic FAIL. 起初我用这种方式https://www.browsersync.io/docs/options/#option-serveStatic失败。 But

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();
          }
        }
     ...

config = {assetExtensions: [ 'js', 'css', 'png', 'jpe?g', 'gif', 'svg', 'eot', 'otf', 'ttc', 'ttf', 'json', 'woff2?' config = {assetExtensions:['js','css','png','jpe?g','gif','svg','eot','otf','ttc','ttf','json ','woff2?' ]} ]}

SUCCESS 成功

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM