0

I'm trying to pull in data from a URL (xyz.com/feed.xml). I'd like to save the XML data from that URL to a new file on my server. I also need to parse or traverse the data coming in then save to a file. Here is my thought process.

Using the following modules: fs(writing), http(requesting the data)

var fs = require('fs');
var http = require('http');
var feedURL = "xyz.com/feed";

http.get(feedURL, function(res) {
    if( itemIsReadable ){
        console.log("Got response: " + res);
    }
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

Is there also a way to check if xml data exists? For example, I only want to write/cache the data if it does not exist since I am using this as a data caching script. The app the data comes from frequently goes down and throws PHP errors so I'd like to check for that.

Thanks for the help. New to nodejs.

1 Answer 1

0

res the response is a read stream. You can pipe it to a file write stream.

res.pipe(fs.createWriteStream('cached-data.xml'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.