Sman
Sman

Reputation: 145

Is there any way to simply store a string or other variable permanently without the use of a DB in node?

Is there any way to store a variable in nodeJS kind of like localStorage but for the server? For example store a JSON object as a string for a small amount of data?

Upvotes: 0

Views: 343

Answers (1)

Jordan Soltman
Jordan Soltman

Reputation: 3883

Totally. Check out the file system module that comes with Node. You can then just write to a file and then read it/change it/etc.

let myData = {
    something: 42,
    else: "a string!"
}

fs.writeFile('db.txt', JSON.stringify(myData), (err) => {
  if (err) throw err;
  console.log('The data has been saved!');
});

You could also consider something like redis which is technically an in-memory database but is very straight forward to use. You should leverage a client like ioredis to easily store and retrieve data.

Upvotes: 3

Related Questions