javascriptxml2js

ENOENT error on xml2js but file did exists


const xml2js = require('xml2js');
const fs = require('fs');

fs.readFile('https://www.tcmb.gov.tr/kurlar/today.xml', (err, data) => {
    if(err) console.log(err);
    var data = data.toString().replace("\ufeff", "");
    xml2js.parseStringPromise(data, (err, res) => {
        if(err){
            console.log(err);
        } else {
            console.log(res);
        }
    });
});
This is my code in nodejs I try to get data on a https link with xml2js first by using the way it says in the npm page pf xml2js it gives some error and when I chechk on web I find solution of using with fs but still geting this error enter image description here

I know the directory exists because if you go to link used in code it shows something but in code just gives error if someone can help I will be very happy


Solution

  • fs can only access file in your system, you should request the URL using http/https or even better try axios

    const axios = require('axios')
    
    axios.get('https://www.tcmb.gov.tr/kurlar/today.xml').then((response) => {
      const data = response.data
      xml2js.parseString(data, (err, res) => {
            if(err){
                console.log(err);
            } else {
                console.log(res);
            }
       })
    })