2016-08-19 9 views
3

いくつかの提供されたパラメータに基づいて生成されたMP3ファイルを出力する単純なExpress 4ベースのAPIがあります。しかし、この一時的なMP3ファイルは、応答が送信された後に削除できないようです。応答が送信された後に一時ファイルを削除する正しい方法

私が持っているもの:

stream 
    .on('end', function() { 
    Fs.unlinkSync(tempFileMp3); 
    }) 
    .on('close', function() { 
    Fs.unlinkSync(tempFileMp3); 
    }) 
; 

しかし、どちら:最初の私は、どちらかendまたはfinishイベントstreamイベントでtempFileMp3を削除することができるだろうと期待していた時に

app.get('/endpoint', function(request, response) { 

    // Distill parameters from request and create tempFileMp3 

    var stat = Fs.statSync(tempFileMp3); 

    response.writeHead(status, { 
    'Content-Type': 'audio/mpeg', 
    'Content-Length': stat.size 
    }); 

    stream = Fs.createReadStream(tempFileMp3); 
    stream.pipe(response); 
}); 

endcloseイベントが発生します。

応答が送信された後、一時的なMP3ファイルを削除するにはどうすればよいですか?

+0

は(関数(){Fs.unlinkSync(tempFileMp3);}、 '仕上げ'); 'stream.onを試し'? https://nodejs.org/api/stream.html#stream_event_finish –

+0

@SwarajGiri実際にそれを試みました。言及を忘れて、無駄に。 – Luke

答えて

1

試行捕捉応答仕上げ:

res.on('finish', function() { 
    // remove temp files 
}); 

res.on('error', function() { 
    // remove temp files 
}); 
+0

'response.finish'イベントは散発的に呼ばれているようです。これらのイベントに関する資料はありますか? – Luke

関連する問題