We know the difference between JSON
and Object Literal
from those two posts
What is the difference between JSON and Object Literal Notation? What's the difference between Javascript Object and JSON object
Today, I discuss this topic with my colleague about using JSON
or Object Literal
as config file in Node.js
.
The opinion of my colleague is to use JSON
.
// message.json
{ "aa": "content",
"enable": true,
"media": {
"event": "Winner",
"message": "Congratulations!"
}
}
// app.js
filepath = "./message.json";
var file = fs.readFileSync(filepath, 'utf-8');
console.log(JSON.parse(file));
However, I insist on the Object Literal
// message.js
exports.value = {
"aa": "content",
"enable": true,
"media":
{
"event": "Winner",
"message": "Congratulations!",
}
}
//app.js
var vv = require('./message.js');
console.log(vv.value.media.message);
Could someone tell me the difference between them? Any one is better or not?