简体   繁体   English

Node.js 读取和写入位于外部 url (http url) 中的 YAML 文件

[英]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 .我正在使用 Node.js,但无法弄清楚如何读取包含数据(YAML 文件)的外部 http url,替换其中的值,并将更新后的值写入位于外部的 YAML 文件网址。

But unfortunately fs module read/write only on local paths.但不幸的是 fs 模块只能在本地路径上读/写。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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