IVnoobSuck
IVnoobSuck

Reputation: 85

Node.js how to save data from terminal

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

Answers (1)

Aalexander
Aalexander

Reputation: 5004

Solution

You can use the fs library to save the data in a file on your filesystem

Here an example

write JSON

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);

read JSON

'use strict';

const fs = require('fs');

let rawdata = fs.readFileSync('student.json');
let student = JSON.parse(rawdata);
console.log(student);

Upvotes: 3

Related Questions