码迷,mamicode.com
首页 > Web开发 > 详细

nodejs读写yaml

时间:2020-07-06 11:08:47      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:writing   const   read   author   use   npm   tags   published   install   

nodejs读写yaml

npm install -g js-yaml

// read.js
const fs = require(‘fs‘);
const yaml = require(‘js-yaml‘);

try {
    let fileContents = fs.readFileSync(‘./data.yaml‘, ‘utf8‘);
    let data = yaml.safeLoad(fileContents);

    console.log(data);
} catch (e) {
    console.log(e);
}



// write.js
const fs = require(‘fs‘);
const yaml = require(‘js-yaml‘);

let data = {
    title: ‘Reading and Writing YAML to a File in Node.js/JavaScript‘,
    ‘url path‘: ‘/reading-and-writing-yaml-to-a-file-in-node-js-javascript‘,
    domain: ‘stackabuse.com‘,
    port: 443,
    ‘is-https‘: true,
    meta: {
        ‘published-at‘: ‘Nov. 1st, 2019‘,
        author: {
            name: ‘Scott Robinson‘,
            contact: ‘scott@stackabuse.com‘
        },
        tags: [
            ‘javascript‘, ‘node.js‘, ‘web development‘
        ]
    }
};

let yamlStr = yaml.safeDump(data);
fs.writeFileSync(‘data-out.yaml‘, yamlStr, ‘utf8‘);

参考:
https://stackabuse.com/reading-and-writing-yaml-to-a-file-in-node-js-javascript/

nodejs读写yaml

标签:writing   const   read   author   use   npm   tags   published   install   

原文地址:https://www.cnblogs.com/itech/p/13253213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!