Mjuice Mjuice - 17 days ago 5
Javascript Question

How do I make use of an xml file in node.js?

In my server.js file, I make an HTTP GET request that is suppose to return xml. When I log the GET request's response to the console, it is gibberish containing lots of question marks and black diamonds as you can see in the photo below:enter image description here

When I take the same url that I'm using in my GET request and I open it in the browser it automatically downloads a gzip file, which after it's unzipped contains a legible xml file with the data (inside my text editor).

How do I get the xml in its correct form inside my server.js file? I need to make use of it in my program, not inside a text editor (obviously).

Here is my GET request:

axios.get('http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l')
.then(function(response) {
console.log(response.data);
});


I've tried to extract the gzip file using the targz library as shown below:

axios.get('http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l')
.then(function(response) {
targz().extract(response.data, '/data', function(err){
if (err) {
console.log('Something is wrong ', err.stack);
}
console.log('Job done!');
});
});


I get an error in the console saying : "Path must be a string without null bytes". Should I be using the extract method from targz or am I just using it incorrectly? I'm trying to "extract" or unzip the response.data.

Answer

Based on this: Simplest way to download and unzip files in Node.js cross-platform?

var feedURL = 'http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l';

var request = require('request'),
    zlib = require('zlib'),
    fs = require('fs'),
    out = fs.createWriteStream('./feed.xml');

request(feedURL).pipe(zlib.createGunzip()).pipe(out);
Comments