【问题标题】:browser-sync disable directory browsing for some sub-directoriesbrowser-sync 禁用某些子目录的目录浏览
【发布时间】:2019-06-11 22:29:38
【问题描述】:

我是浏览器同步和 gulp 的新手,但根据使用情况,我试图让我的 http-server 可以通过网络服务器访问。此外,我想通过对文件属性使用否定来排除一些要隐藏或不被浏览的目录,但它不起作用......我的主要目标是定义一些目录以一如既往地提供 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
        }    
        });
});

【问题讨论】:

    标签: node.js gulp browser-sync


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 感谢您的回答,但有一点我不清楚; “/mule_local_exchange/d2c/PUBLIC”是什么意思。所有文件仍然可以从所有目录中获得。”这句话在这里?如果可能的话,你能简要解释一下吗?
      • 当您将浏览器指向 browserSync url 时,例如localhost:3000,它只会显示baseDirfirst 文件夹的列表。
      • ... 但是baseDir 中列出的所有文件夹中的文件仍将通过直接链接在您的项目中可用。如果您在/some/other/dir 中有一个文件main.js,它将以localhost:3000/main.js 的形式提供。
      猜你喜欢
      • 2020-01-29
      • 2011-02-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      相关资源
      最近更新 更多