【问题标题】:using a wildcard/glob/minimatch in an fs.readFile (inside express app)在 fs.readFile 中使用通配符/glob/minimatch(在 express 应用程序内)
【发布时间】:2013-12-19 10:43:33
【问题描述】:

我有一个(迷你)快递应用。基本上只是显示覆盖结果。在里面我有:

app.get('/coverage', function(req, res) {
   fs.readFile(path.join(__dirname, '/coverage', 'PhantomJS 1.9.2 (Linux)', 'lcov-report', 'index.html'), 'utf8', function(err, content) {

        if(!err) {
            res.send(content);
        } else {
            res.setHeader({ status: '404' });
            res.send('');
        }
    });

});

我的问题是测试运行器在创建测试覆盖率报告时可以更改文件夹路径,可以是 Phantom 1.9.3 或类似的东西。所以我想我需要在中间(在覆盖范围和 lcov-report 之间)使用某种通配符来构建路径。

如何做到这一点?

【问题讨论】:

    标签: node.js express node.js-connect


    【解决方案1】:

    您不能在 Node 中使用本机,但您可以为此目的使用 3rd 方模块。
    例如,使用glob 模块:

    var glob = require('glob');
    
    app.get('/coverage', function(req, res) {
       glob(path.join(__dirname, '/coverage', 'PhantomJS *', 'lcov-report', 'index.html'), function(err, matches) {
          if (err) {
             // handle error
          }
    
          fs.readFile(matches[0], 'utf8', function(err, content) {
             if(!err) {
                res.send(content);
             } else {
                res.statusCode(404);
                res.send('');
             }
          });
       });
    });
    

    我还没有测试过,但我想它会起作用的!
    别忘了处理错误,孩子们!

    【讨论】:

    • 如果有人在家里尝试这个,他应该注意他必须处理错误等。
    • 将其添加到答案中;)
    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    相关资源
    最近更新 更多