1
ノードを使用してリモートzipファイルをディスクに保存できません。 私はrequest
ライブラリを使用してリクエストしています。要求が成功した場合は、ディスクに書き込むために、zipファイル を要求します。 エラー処理とファイルの正しい書き換えを正しく組み合わせることができません。node.js.要求されたzipファイルをディスクに保存する際の問題
私は次の操作を実行したい:
request.get('https://example.com/example.zip', {
'auth': { 'bearer': accessToken },
}, function(error, response, body) {
// shortcircuit with notification if unsuccessful request
if (error) { return handleError() }
// I want to save to file only if no errors
// obviously this doesn't work because body is not a stream
// but this is where I want to handle it.
body.pipe(fs.createWriteStream('./output.zip'));
});
私は、要求が直接として次の私ができるパイプを知っているが、私はまともなエラー処理を取得することはできません。エラーコールバックの404エラーのために発生しません、と私は要求をキャッチした場合と!response.ok
空の出力ファイルがまだディスク
request.get('https://example.com/example.zip', {
'auth': { 'bearer': accessToken },
})
.on('error', handleError)
.pipe(fs.createWriteStream('./output.zip'));
おかげで失敗ためのオプションを持っているrequest-promise moduleをチェックアウトをお勧めします。追加のヒントのための歓声も、それらをチェックします.. – patnz