【发布时间】:2021-02-20 06:11:57
【问题描述】:
我有一个生成文件的内部 HTTP Post API,每个文件大小为 5-10mb。此服务不可修改。
我想通过基于 Node.js+Express 的公共 API 来“代理”这个文件下载。但是,我想不出最好的方法。
我想我可以使用 Axios 将此文件下载到 Node.js API 容器中的临时文件中,但这似乎很容易出现这些临时文件可能堆积并需要稍后清理的问题。有没有办法实现这样的文件下载-> 在不创建临时文件的情况下进一步发送给客户端? 或者如果临时文件是不可避免的,那么最有效和“干净”的方法是什么?
router.post('/route/:someid',
[someRequestVerificationMiddleware],
(req, res, next) => {
const myFileId = req.params.someid;
const downloadRequestParams= {
"id": myFileId
};
let dlPromise = axios.post(`http://myinternalservice:80`,
downloadRequestParams, {responseType: "stream"});
dlPromise.then(response => {
try {
let filename = response.headers["x-result-filename"];
//
// What would be the most efficient way to return the received file
// from response data to the client calling this route without creating
// too much garbage?
//
} catch (e) {
console.error(e);
}
})
.catch(e=>{
console.error(e);
res.status(500);
})
.finally(() => {
next();
})
});
module.exports = router;
【问题讨论】:
-
仅供参考,您不会在发送响应的请求处理程序中调用
next()。当您希望路由继续到其他路由处理程序时,您仅在中间件中调用next()- 其中一个将发送响应。