简体   繁体   中英

Node.js reading and writing into YAML file located into external url (http url)

I'm using Node.js and I'm having trouble figuring out how could I read external http url which has data (a YAML file) , replace a value in it, and write the updated value to the YAML file located in the external URL .

But unfortunately fs module read/write only on local paths.

Any idea about how to resolve this issue?

Thanks!

const yaml = require('js-yaml');

const fs = require('fs')

let doc = yaml.safeLoad(fs.readFileSync('http://xxx', 'utf8'));

doc.General.Greeting = newGreet;

fs.writeFile('http://xxx', yaml.safeDump(doc), (err) => {
    if (err) {
        console.log(err);
    }
});
const yaml = require('js-yaml');
const request = require('request');
const getSchema = (url) => { 
    return new Promise((resolve, reject) => {
        request.get(url, (error, response, body) => {
            if (!error && response.statusCode == 200) {
                return resolve(body);
            }
            return reject({
                message: "Invalid URL!",
                stack: error ? error.stack : null
            });
        });
    });
}
const yamlToJson = (content) => yaml.safeLoad(content);

//put this code in async block
const yamlSchema = await getSchema(schemaURL);
const jsonSchema = yamlToJson(yamlSchema);
console.log(jsonSchema);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM