Well first of all, I hope this is just for educational purposes, and you do not plan to manage user accounts this way, storing plaintext passwords in a json file.
Second, you seem to misuse the structure of JSON. Why to have user1
, user2
, user3
keys when you can have an array? It would be much better if you'd arrange this data like this:
{
"users": [
{
"username": "Alice",
"anything-but-not-plaintext-password": "Puppies and kittens"
},
{
"username": "Bob",
"anything-but-not-plaintext-password": "Cheese nuggets"
}
]
}
Finally, you can write a json to a file in nodejs using fs.writeFileSync:
var fs = require('fs');
var jsonContent = {}; // Here is your loader and modifier code
var jsonString = JSON.stringify(jsonContent, null, 4); // Pretty printed
fs.writeFileSync("./actions/writeTo.json", jsonString);
But still, this is how you must not handle user credentials. Here is a pretty good tutorial on the topic: Node.js authentication series: Passport local strategy using email and password.