|- monday/
|- outer.js
|- layer1/
|- index.js
|- layer2/
|- index.js
|- data.js
|- layer3/
|- index.js
layer1/index.js export a module that return hello world from layer 1 !layer2/data.js export a module that has:datadata is an array contain 5 objectsname:anyName and age:anyNumberlayer2/index.js import data that previously made.layer2/index.js export a module that when called : print to the console this format:User name is : anyName User age is : anyAge
layer3/index.js import data from layer2/data.jslayer3/index.js export a module that contain a method that print to the consoleUser <anyName> is oldest, age <anyAge> User <anyName> is youngest, age <anyAge>
outer.js import all module and execute in an order that print to the console. You may also have to add in some more command to complete the formatHello world from layer1 ! ------- This is the list of Users : User name is <anyName>, User age is : <anyAge> ======== User <anyName> is oldest, age <anyAge> User <anyName> is youngest, age <anyAge>
In reality, we will not commonly embeded data into our js file like the previous exercise.
We would store our data in a separated .json file and import it as needed. This would help other module able to access to the one database , one source of truth.
When reading data from a .json file the content should first be parse to a javacript object prior to further javascript operation.
JSON.parse();
Vice versa , to write to a .json file, a javascript object must first be transformed into a JSON string prior to save.
JSON.stringify();
"data":{
"name":"abc"
}
data: {
name: "abc";
}
db.json with the data from data.js in previous exercise at root of our project structure.layer2/index and layer3/index replace the use of data.js and change to db.json insteadJson file format need to be turn to JS object before usage.... require("fs")
... fs.read... or fs.readSync...
... callBack???
... const data = ...? JSON.parse() ???