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

Node.js returning JSON as file

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.



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

×