浏览器同步禁用某些子目录的目录浏览



我是使用浏览器同步和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
        }    
        });
});

我无法禁用目录列表; 但是变通办法是,如果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();
                        }

您可以为浏览器同步定义多个baseDir,目录列表仅适用于第一个:

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

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

最新更新