Last active
June 28, 2016 08:26
-
-
Save RafalWilinski/8053599d086c0e649bd1aa13d15992fb to your computer and use it in GitHub Desktop.
StackOverflow #38070287 Code Answer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/38070287/nodejs-how-to-manage-username-and-task-management-without-using-external-datab?noredirect=1#comment63578743_38070287 | |
// On server start load data from file | |
var storage = {tasks: []}; | |
fs.readFile('database.json', function(err, data) { | |
if(err) throw new Error(err); | |
storage = JSON.parse(data); | |
}); | |
// On incoming request | |
if (query.id == '1') { | |
// Add task to variable storage | |
storage.tasks.push({taskId: query.id, taskName: query.name}); | |
} | |
else if (query.id == '2') { | |
res.json(storage); | |
} | |
// Update database persistent file | |
fs.writeFile('database.json', JSON.stringify(storage), function (err) { | |
if (err) throw new Error('Error while serializing DB'); else console.log('DB updated!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment