Node.js returning JSON as file
Problem
I'm writing a node.js app and the most curious thing is happening when I try to get data from a third party API. I get teh JSON result as a file. in console it outs the text correctly, but when I hit it from the browser it downloads a file with json string in it. I've tried doing JSON.stringify but that just prints out hex I believe.
How do I tell node.js to output the contents of the result to the browser?
https.get({ host: 'api.bookshare.org', path: '/book/searchFTS/"+keyword+"/page/1/limit/250/format/json?api_key=2msxgk595nke8rhwmbzh8sut' }, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.log(d);
});
}).on('error', function(e) {
console.error(e);
});
Problem courtesy of: Faisal Abid
Solution
Chrome was treating the content-type header text/json as a file. Setting it to text/plain did the trick.
Solution courtesy of: Faisal Abid
Discussion
View additional discussion.