This code seems behave Ok, but when I try read the file (someFile.json) in the last line simply does not work. :-( Please some clue. Although I know the issue is complex.
var fs = require('fs');
var request = require("request")
var dataFile = 'data/someFile.json'
var url = "https://api.someUrl"
var file = fs.createWriteStream(dataFile);
request(url).pipe(file);
file.on('finish',function(){
console.log('file download to ',dataFile)
file.destroy();
//file.closeSync;
file.on('close', function(){ console.log('File Closed ')})
})
var datos = fs.readFileSync(dataFile, 'utf8');
You are trying to read the file BEFORE it has been written. The writeStream you are writing to is asynchronous. That means it completes some indeterminate time in the future. Meanwhile, the rest of your code continues to run and thus you try to read it before the file has been finished.
In the code you have, here's the sequence of events.
request()
request()
to your stream with .pipe()
.finish
event handler on the stream.fs.readFileSync()
to read the filerequest()
and is sent to the write stream where it may be buffered.request()
and is sent to the write stream where it may be buffered.finish
event occurs.close
event handler.close
event occurs.You can likely use stream notifications to do what you want, but to just test out the file, you can put the fs.readFileSync()
in the completion handle for the close event on the write stream.
const fs = require('fs');
const request = require("request")
const dataFile = 'data/someFile.json'
const url = "https://api.someUrl"
const file = fs.createWriteStream(dataFile);
request(url).pipe(file);
file.on('finish',function(){
console.log('file download to ',dataFile)
}).on('close', function(){
console.log('File Closed ');
// file is available for reading now
var datos = fs.readFileSync(dataFile, 'utf8');
console.log(datos);
});
Also, you do not need to .destroy()
the stream. By default, it will be set to autoClose
which will close automatically when the stream you piped to it is done. And, you do not want your .on('close', ...)
event handler inside your finish
error handler.
In this new suggested code, here's the sequence of events:
request()
request()
to your stream with .pipe()
.finish
event handler on the stream.close
event handler.request()
and is sent to the write stream where it may be buffered.request()
and is sent to the write stream where it may be buffered.finish
event occurs.close
event occurs.fs.readFileSync()
to read the file