Reputation: 85
I finally managed to work functionating api with node.js. Is there a way to save the tables I get from the other system via the api? preferable in json format. Right now I only see them in the terminal but I would like to have the option to save them in a folder of my choice for later use.Is there any universal code out there for that?
Upvotes: 1
Views: 1222
Reputation: 5004
You can use the fs library to save the data in a file on your filesystem
Here an example
Adjust the path in fs.writeFileSync('student-2.json', data);
the first param and you need write access to the directory.
'use strict';
const fs = require('fs');
let student = {
name: 'Mike',
age: 23,
gender: 'Male',
department: 'English',
car: 'Honda'
};
let data = JSON.stringify(student);
fs.writeFileSync('student-2.json', data);
'use strict';
const fs = require('fs');
let rawdata = fs.readFileSync('student.json');
let student = JSON.parse(rawdata);
console.log(student);
Upvotes: 3