Question

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?

Était-ce utile?

La solution

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!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top