admin管理员组文章数量:1316842
I need to write an application with Node JS which given a link to a json file e.g .json (The link doesn't open the file it opens a download), the program simply downloads the object and then prints it out. How can this be achieved? This is what I have so far and it doesn't seem to be working:
var checkIfPhishing = function(urlToPrint){
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("SiteObject.json");
var request = http.get(urlToPrint, function(response) {
response.pipe(file);});
var siteObj= fs.readFileSync("SiteObject.json");
console.log(siteObj);
};
Thank you!
I need to write an application with Node JS which given a link to a json file e.g http://data.phishtank./data/online-valid.json (The link doesn't open the file it opens a download), the program simply downloads the object and then prints it out. How can this be achieved? This is what I have so far and it doesn't seem to be working:
var checkIfPhishing = function(urlToPrint){
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("SiteObject.json");
var request = http.get(urlToPrint, function(response) {
response.pipe(file);});
var siteObj= fs.readFileSync("SiteObject.json");
console.log(siteObj);
};
Thank you!
Share Improve this question asked May 23, 2017 at 19:13 NivolasNivolas 932 gold badges2 silver badges10 bronze badges 1- 1 At first put the requires out of the function. That will double your performance... – Jonas Wilms Commented May 23, 2017 at 19:24
1 Answer
Reset to default 6You cannot mix up async and sync reads and writes. In your case you start Streaming the data from the other server to yours, but then you already start the sync read. Which blocks the thread so the stream will be processed after you've read your 0 byte file...
So you need to store the stream in a variable first, then on finish log that stream.
So, simply, do something like this:
let data = "";
const request = http.get( urlToPrint, function(response) {
response
.on("data", append => data += append )
.on("error", e => console.log(e) )
.on("end", ()=> console.log(data) );
});
Store the asynchronously provided chunks of the stream in a variable, if the stream finishes, log that string.
If you want to store it too:
const http = require('http');
const fs = require('fs');
function checkIfPhishing(urlToPrint){
const file = fs.createWriteStream("SiteObject.json");
const request = http.get(urlToPrint, function(response) {
response.on("finish",function(){
console.log( fs.readFileSync("SiteObject.json",{encoding:"utf8"}));
}).pipe(file);
});
}
This is exactly like your code, but it waits for the stream to finish before it reads synchronously...
However, note that the sync read will slow down the whole thing, so you might directly stream to the console/a browser..
本文标签: javascriptDownload a json file from a link using Node jsStack Overflow
版权声明:本文标题:javascript - Download a json file from a link using Node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742007757a2412303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论