通过管道发送多个文件 [英] sending multiple files down the pipe

查看:66
本文介绍了通过管道发送多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Express 4,现在我有这样的东西:

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

}

但这不起作用.如何使用Express服务多个文件?注意:不仅是目录中的所有文件(如示例中一样)-可能可以通过app.use; express.static完成,而是特定的文件集(例如,我可能需要从bower.json获取文件列表)

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)

推荐答案

除非您使用自己的特殊格式(标准的多部分格式或其他格式),然后解析该格式,否则无法在单个响应中发送多个类似的文件客户端(例如,通过XHR).

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

最简单的解决方法可能是存档(zip,7zip,tarball等)文件,然后提供该存档.但是,这假定您希望用户下载它而不使用浏览器中的文件(除非您在浏览器中具有zip等解析器并使用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).

这篇关于通过管道发送多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆