I'm trying to download a file with express js, but I'm having some trouble.
Below is the function:
var download = function(uri, filename, callback) {
request
.get(uri)
.on('response', function (response) {
var ext = response.headers['content-type'].split('/');
filename += '.' + ext[1];
})
.pipe(fs.createWriteStream(filename));
};
The problem is that I don't know the extension of the file, so I need to get it in response headers but the ext[1]
value doesn't increment my filename with the file extension. I can't even set a variable by reference to do it outside the function.