get data json data from browser on nodejs code example
Example 1: HOW WRITE AND SAVE JSON FILE IN NODEJS
'use strict';
const fs = require('fs');
let student = {
name: 'Mike',
age: 23,
gender: 'Male',
department: 'English',
car: 'Honda'
};
let data = JSON.stringify(student, null, 2);
fs.writeFile('student-3.json', data, (err) => {
if (err) throw err;
console.log('Data written to file');
});
console.log('This is after the write call');
Example 2: nodejs store json from web api
const http = require("http");
var url = 'https://example.com/file.json';
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var data = JSON.parse(body);
console.log("Got a response: ", data.value);
});
}).on('error', function(e){
console.log("Got an error: ", e);
});