我正在构建一个 NodeJs SOAP 客户端.最初,我想象服务器(即节点 SOAP 客户端)将允许通过 REST API 下载文档(REST API 已通过身份验证).在 Google 和 SO 上花了很多时间之后,看起来这是不可能的.
I am building a NodeJs SOAP client. Originally, I imagined the server (ie the node SOAP client) would allow downloading documents through a REST API (the REST API is authenticated). After a good deal of time on Google and SO, looks like that is not possible.
这意味着当请求下载文档时,我必须对文档进行 SOAP 调用,并通过 AJAX 向 REST 客户端返回一个 URL.
That means when a document download is requested, I'll have to make a SOAP call for the document and return a URL to the REST client via AJAX.
为了做到这一点,我需要:
In order to do this I'll need to:
这是我的问题:
如果没有,我是否可以仅使用 NodeJs 文件系统来执行此操作,并使用 Express static模块?基本上我们会将静态模块修改为如下所示:
If not, can I do this just using the NodeJs File System, and Express static module? Basically we would modify the static module to look like this: return function static(req, res, next) { if ('GET' != req.method && 'HEAD' != req.method) return next(); var path = parse(req).pathname; var pause = utils.pause(req); /* My Added Code Here */ res.on('end', function(){ // delete file based on req URL }) /* end additions */ function resume() { next(); pause.resume(); } function directory() { if (!redirect) return resume(); var pathname = url.parse(req.originalUrl).pathname; res.statusCode = 301; res.setHeader('Location', pathname + '/'); res.end('Redirecting to ' + utils.escape(pathname) + '/'); } function error(err) { if (404 == err.status) return resume(); next(err); } send(req, path) .maxage(options.maxAge || 0) .root(root) .hidden(options.hidden) .on('error', error) .on('directory', directory) .pipe(res); };
res.on('end',... 是否有效?或者,我是否应该创建一些中间件来为指向临时文件的 URL 执行此操作?
Is res.on('end',... vaild? Alternatively,should I create some middleware that does this for URLs pointing to the temporary files?
推荐答案发现两个 SO 问题可以回答我的问题.所以显然我们不需要使用 express.static 中间件.我们只需要文件系统来下载文件:
Found two SO questions that answer my question. So apparently we don't need to use the express.static middleware. We just need the filesystem to download a file:
app.get('/download', function(req, res){ var file = __dirname + '/upload-folder/dramaticpenguin.MOV'; res.download(file); // Set disposition and send it. });如果我们想流然后删除,请遵循:
app.get('/download', function(req, res){ var stream = fs.createReadStream('<filepath>/example.pdf', {bufferSize: 64 * 1024}) stream.pipe(res); var had_error = false; stream.on('error', function(err){ had_error = true; }); stream.on('close', function(){ if (!had_error) fs.unlink('<filepath>/example.pdf'); });更多推荐
使用 NodeJs 提供临时文件
发布评论