Alon and Idan Alon and Idan - 17 days ago 6
Node.js Question

request() and fs.createReadStream() not returning the "right" values

I'm trying to scrape the content of a webpage with Node.js and compare it to a copy of the same page I have stored as a file, like so:

var fs = require("fs");
var request = require("request");
var archive = ["./archive.html", "http://praguerace.com/comic/archive"];

request(archive[1], //request Prague Race's archive
function (error, response, body) {
if (fs.createReadStream(archive[0]) == body) //if no change occurred
console.log("checkpoint 1");
else
console.log("checkpoint 2");
}
);


I already have the Request module installed (properly), and am not using Express.

The problem is that the script keeps printing "checkpoint 2", it's as if the response I get from the server I'm trying to scrape keeps changing, or Node doesn't know what
==
means.

Answer

fs.createReadStream(archive[0]) return a stream and not the content

Use stream event to retrieve the content, readStream.on('open', 'data', etc...

Or just use readFile or readFileSync

Async version :

request(archive[1], //request Prague Race's archive
function (error, response, body) {
   fs.readFile(archive[0], "utf8", function(err, data){
     if(data  == body)
       ....
     else
        ....
    });
});

Sync version

request(archive[1], //request Prague Race's archive
function (error, response, body) {
     if(fs.readFileSync(archive[0], "utf8")  == body)
       ....
     else
        ....
});
Comments