6

we're using express 4 and right now I have something like this:

var express = require('express'),
    router = express.Router();

router.get('/local_modules/*', function (req, res, next) {
   var moduleName = req.url.match(/local_modules\/(.*?)\//).pop(1)
   res.sendFile(filePath + '.js');
}

and I wanna do something more like:

router.get('/local_modules/*', function (req, res, next) {
   var moduleDir = req.url.match(/local_modules\/(.*?)\//).pop(1)

   fs.readdir(moduleDir, function(err, files) { 
     files.forEach(function(f) {
         res.sendFile(path.join(moduleDir, f));
     })
   }

}

But that doesn't work. How can I serve multiple files with express? Note: not just all files in a directory (like in the example) - which probably can be done with app.use; express.static , but specific set of files (e.g. I may need to get the list of files from bower.json)

iLemming
  • 34,477
  • 60
  • 195
  • 309
  • You could attempt to concat all the files together and then send that as the response. Assuming you're only sending js that is. – idbehold Jun 18 '15 at 18:33

1 Answers1

7

There is no way to send multiple files like that in a single response, unless you use your own special formatting (standard multipart or otherwise) and then parse that on the client side (e.g. via XHR).

Probably the easiest workaround would be to archive (zip, 7zip, tarball, etc.) the files and then serve that archive instead. This assumes however that you want the user to download it and not use the files in the browser (unless you have a zip, etc. parser in the browser and use XHR).

mscdex
  • 104,356
  • 15
  • 192
  • 153