Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js returning JSON as file

Node.js returning JSON as file


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: '', 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) {

}).on('error', function(e) {
Problem courtesy of: Faisal Abid


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


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js returning JSON as file


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription