I have a Node.js web server running on an embedded Linux system. For authentication I'm currently using Passport's Local Strategy, and the user account information is stored in the Node script itself as follows.
var users = [
{ id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' }
, { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' }
];
It works, but I'd like to store the usernames and passwords in a separate file and be able to add, delete and change users through the web browser interface. I don't want to use a database server to store the user accounts because I feel it's too much overhead for my embedded system.
Does anyone know of a good way to store the user accounts in a separate file with possibly a library to help update the accounts?
EDIT: The Node server is running on electronics hardware which will have very few (2-20 maybe) users, so there shouldn't be a problem with people stepping on each other doing user list updates. After initial set-up, the user list won't change much.
You're probably already aware that storing passwords in plaintext is a bad idea; at minimum, I would recommend hashing with bcrypt. However, that's not what the question's about, so we'll move on. :)
You have a few options:
JSON.stringify
and JSON.parse
. Load up the data into memory, and when it gets changed, serialize it back to disk.A very simple approach would be to just read from a JSON file. On updates then write to accounts.json.(process id).tmp
and rename the file to accounts.json
. There's a small risk you'll lose an update if two come in at the same time, but maybe you can live with that. (If you can't then use a "real" database for some value of "real").
You can use JSON.stringify(data)
to convert a data structure to JSON and JSON.parse(json)
to convert the JSON string from the file to a data structure.
If your service/server has little enough use that you can block you can read the accounts with
var data = JSON.parse(fs.readFileSync( file_name ) );
I am totally unaware of all the three tags used by you, but I got attracted by your question wherein you are asking about a simple text file based database. Please don't mind if you find my answer irrelevant as I am from a totally different background.
Have a look at MongoDB. It is a document-oriented DBMS which is very useful when dealing with large no of documents.
MongoDB uses BSON, a binary object format similar to, but more expressive than, JSON.It is a NOSQL database that stores structured data in BSON format with dynamic schemas instead of storing data in tables as is done in a "classical" RDBMS.
If you find it appealing you can visit this link for more detail : MongoDb