Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  194] [ 2]  / answers: 1 / hits: 13723  / 9 Years ago, fri, april 24, 2015, 12:00:00

I'm a node.js beginner. I'm trying to request a json file from a url (i.e 'http://www.example.com/sample_data.json').
My goal is to download/request the file only once when the server loads and then save it on the client side so I can manipulate/change it locally.
I tried



var file = request('http//exmaple.com/sample_data.json')


but it returns an import module error.
If anyone could give me a start that would be great!
thanks


More From » json

 Answers
4

To do that i would use the request module.



var request = require('request');
request('http//exmaple.com/sample_data.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var importedJSON = JSON.parse(body);
console.log(importedJSON);
}
})


For more information about the module check this link: https://github.com/request/request


[#37655] Thursday, April 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aiyannam

Total Points: 642
Total Questions: 96
Total Answers: 102

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;