0
How do I request an external file with Nodejs? In case it would be an XML... http://179.127. 4.122:8449/played.html? sid=1&type=xml
0
How do I request an external file with Nodejs? In case it would be an XML... http://179.127. 4.122:8449/played.html? sid=1&type=xml
1
You can do the following, use the module http to give a get and convert the path in xml for a object. I created as a Promise so you can use await in a Function async to wait for the get.
const http = require('http');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
const concat = require('concat-stream');
parser.on('error', function(err) { console.log('Parser error', err); });
const getXml = async()=>{
return new Promise((resolve, reject) =>{
http.get('http://179.127.4.122:8449/played.html?sid=1&type=xml', function(resp) {
resp.on('error', function(err) {
reject(err)
})
resp.pipe(concat(function(buffer) {
const str = buffer.toString();
parser.parseString(str, function(err, result) {
if(err){
reject(err);
}else{
console.log(result);
resolve(result);
}
});
}));
});
})
}
//export default getXml
async function init(){
const data = await getXml()
console.log(data);
}
init()
Browser other questions tagged javascript node.js
You are not signed in. Login or sign up in order to post.
Thank you so much, you are an angel in my life hahaha s2
– Erick Pereira
Opa tamo ai, do not forget brand as the right answer, hug.
– Chance