using a wildcard/glob/minimatch in an fs.readFile (inside express app)

6.9k views Asked by At

I got a (mini) express app. basically just showing coverage results. in it I have:

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

});

My problem is that the test runner, while creating the test coverage report can change the folder path, can be Phantom 1.9.3 or something similiar. So I guess I need to build the path with somekind of wildcard in the middle (between coverage and lcov-report).

How can this be achieved?

1

There are 1 answers

2
gustavohenke On BEST ANSWER

Natively in Node you can't, but you may use a 3rd party module for this purpose.
For example, using the glob module:

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

I haven't tested it, but I suppose it's gonna work!
and don't forget to handle errors, kids!