How to Request External Nodejs File

Asked

Viewed 150 times

0

1 answer

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()

Reference

  • Thank you so much, you are an angel in my life hahaha s2

  • Opa tamo ai, do not forget brand as the right answer, hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.