working with yaml in node js

Solutions on MaxInterview for working with yaml in node js by the best coders in the world

showing results for - "working with yaml in node js"
Valentina
08 Jul 2018
1/*
2    This code comes from Vincent Lab
3    And it has a video version linked here: https://www.youtube.com/watch?v=loX_JpMnodA
4*/
5
6// Import dependencies
7const fs = require("fs");
8const YAML = require("js-yaml");
9
10// Get YAML or throw exception on error
11try {
12
13    // Load the YAML
14    const raw = fs.readFileSync("data.yaml");
15    const data = YAML.safeLoad(raw);
16
17    // Show the YAML
18    console.log(data);
19
20    // Modify the YAML
21    data.customer.first_name = "Bob"; // Dorothy
22
23    // Saved the YAML
24    const yaml = YAML.safeDump(data);
25    fs.writeFileSync("data.yaml", yaml, function (err, file) {
26        if (err) throw err;
27        console.log("Saved!");
28    });
29
30} catch (ex) {
31    // Show error
32    console.log(ex);
33}