Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending multiple files down the pipe

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)

like image 405
iLemming Avatar asked Jun 18 '15 18:06

iLemming


1 Answers

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).

like image 87
mscdex Avatar answered Nov 19 '22 10:11

mscdex