[英]Piping readable stream using superagent
我正在嘗試創建一個multer中間件來管理來自客戶端的流文件,通過superagent管理第三方。
const superagent = require('superagent');
const multer = require('multer');
// my middleware
function streamstorage(){
function StreamStorage(){}
StreamStorage.prototype._handleFile = function(req, file, cb){
console.log(file.stream) // <-- is readable stream
const post = superagent.post('www.some-other-host.com');
file.stream.pipe(file.stream);
// need to call cb(null, {some: data}); but how
// do i get/handle the response from this post request?
}
return new StreamStorage()
}
const streamMiddleware = {
storage: streamstorage()
}
app.post('/someupload', streamMiddleware.single('rawimage'), function(req, res){
res.send('some token based on the superagent response')
});
我認為這似乎有效,但我不知道如何處理來自superagent POST
請求的響應,因為我需要返回從superagent請求收到的令牌。
我已經嘗試過post.end(fn...)
但顯然end
和pipe
不能同時使用 。 我覺得我誤解了管道是如何工作的,或者我想要做的事情是否切合實際。
Superagent的.pipe()
方法用於下載(將數據從遠程主機傳輸到本地應用程序)。
您似乎需要在另一個方向上進行管道傳輸:從應用程序上傳到遠程服務器。 在superagent(從v2.1開始)沒有方法,它需要一種不同的方法。
您有兩種選擇:
最簡單,效率最低的是:
告訴multer緩沖/保存文件,然后使用.attach()
上傳整個文件。
更難的是“手動”“管道”文件:
data
事件,並使用每個數據塊調用superagent的.write()
方法。 end
事件,並調用superagent的.end()
方法來讀取服務器的響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.