I'm working on my first node project and I'm using requests
to get data from the Twitch.tv API. I would like to avoid having to first write the output of the .get
to a file then read it before I can use it. However, when I console.out
the request.get
directly I don't get the same output as when I write to a file. I'm confused as to why this is. Is there a way to get the same output from the .get
when outputting to the console and a file?
这给出了正确的输出
request.get('https://api.twitch.tv/kraken/users?login=' + args[1], {
'headers' : {
'Client-ID' : 'xxxxxxxxxxxxxxxxxxx',
'Accept' : 'application/vnd.twitchtv.v5+json'
}
}).pipe(fs.createWriteStream('output.json')).on('finish', function() {
let rawData = fs.readFileSync('output.json');
console.log(JSON.parse(rawData));
});
虽然这给出了不包含任何所需数据的输出
console.log(request.get('https://api.twitch.tv/kraken/users?login=' + args[1], {
'headers' : {
'Client-ID' : 'xxxxxxxxxxxxxxxxxxx',
'Accept' : 'application/vnd.twitchtv.v5+json'
}
}));