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.
A very simple approach would be to just read from a JSON file. On updates then write to
accounts.json.(process id).tmpand rename the file toaccounts.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 andJSON.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