browser-sync 禁用某些子目录的目录浏览

browser-sync disable directory browsing for some sub-directories

我是浏览器同步和 gulp 的新手,但根据使用情况,我正在尝试通过网络服务器访问我的 http 服务器。此外,我想通过对文件属性使用否定来排除一些要隐藏或不被浏览的目录,但它不起作用......我的主要目标是定义一些目录以始终为 404 提供他们所要求的任何东西......

有人可以检查一下吗?如果可能的话,这是我的 gulp 实现:

var gulp        = require('gulp');
var browserSync = require('browser-sync').create();
var files = ['d2cvib/output/**/*.{xml}','!d2cvib/changed-list/**'];
// Static server
gulp.task('browser-sync', function() {
    browserSync.init({files,
        port: 8203,
        server: {
            baseDir: "/mule_local_exchange/d2c/",
            middleware: [
            function(req, res, next) {
                const user = 'd2c';
                const pass = 'd2cweb';
                let authorized = false;
                // See if authorization exist in the request and matches username/password
                if (req.headers.authorization) {
                    const credentials = new Buffer(req.headers.authorization.replace('Basic ', ''), 'base64').toString().split(/:(.*)/)
                      if (credentials[0] === user && credentials[1] === pass) {
                          authorized = true;
                      }
                }
                if (authorized) {
                    // Proceed to fulfill the request
                    next();
                } else {
                    // Authorization doesn't exist / doesn't match, send authorization request in the response header
                    res.writeHead(401, {'WWW-Authenticate': 'Basic realm="Authenticate"'})
                    res.end();
                }
            }

        ],
        directory: true
        }    
        });
});

我无法禁用目录列表;但是 work-aroundly 如果 HTTP GET 询问了一些目录,我已经禁用了响应代码,不是 %100 干净的解决方案,但适用于我的情况:

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

//For conditions of rest-uri patterns
function buildSearch(substrings) {
  return new RegExp(
    substrings
    .map(function (s) {return s.replace(/[.*+?^${}()|[\]\]/g, '\$&');})
    .join('{1,}|') + '{1,}'
  );
}


gulp.task('clear-cache', function() {
  // Or, just call this for everything
  cache.clearAll();
});

// Static server
gulp.task('browser-sync', function () {
    browserSync.init({
        port: 8203,
        server: {
            baseDir: "/mule_local_exchange/d2c/",
            middleware: [
                function (req, res, next) {
                    const user = 'd2c';
                    const pass = 'd2cweb';
                    var pattern = buildSearch(['changed-list','current', 'changed_list']);
                    let authorized = false;
                    // See if authorization exist in the request and matches username/password
                    if (req.headers.authorization) {
                        const credentials = new Buffer(req.headers.authorization.replace('Basic ', ''), 'base64').toString().split(/:(.*)/)
                        if (credentials[0] === user && credentials[1] === pass) {
                            authorized = true;
                        }
                    }
                    if (authorized) {

                        if (pattern.test(req.url)) { //400 for not required directories
                            res.writeHead(400, {'Response':'Bad-request'})
                            res.end();
                        } else { // Proceed to fulfill the request
                            next();
                        }
                    } else {
                        // Authorization doesn't exist / doesn't match, send authorization request in the response header
                        res.writeHead(401, {
                            'WWW-Authenticate': 'Basic realm="Authenticate"'
                        })
                        //res.send(401,{ 'Authentication' : 'Failed' })
                        res.end();
                    }
                }
            ],
            directory: true
        }
    });
});

该部分完成工作:

if (pattern.test(req.url)) { //400 for not required directories
                            res.writeHead(400, {'Response':'Bad-request'})
                            res.end();
                        }

您可以为 browsers-sync 定义多个 baseDir,目录列表将仅对第一个可用:

baseDir: ["/mule_local_exchange/d2c/PUBLIC", "/mule_local_exchange/d2c/", "/some/other/dir"],
directory: true

在此示例中,目录列表将仅显示 /mule_local_exchange/d2c/PUBLIC" 的内容。所有目录中的所有文件仍然可用。