从node.js中的stack-overlfow api获取垃圾JSON

我正在使用stackoverflow APIsearch一个问题。 这是我的代码:

app.get('/sof',function(req,res){ var ark; request("https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&q=python%20best%20practices&site=stackoverflow", function(error, response, body) { ark = body; console.log(body); res.send(ark); }); }); 

但是,我在浏览器和日志中得到垃圾值bot。

我怎样才能解决这个问题? 其他一切都运行良好。

我也在用parsing器:

 var bodyParser = require('body-parser'); app.use(bodyParser.json()); 

编辑:这里是@Wainage在解释中解释的代码。

 app.get('/sof', function(req, res){ request.get({ url: "https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&q=python%20best%20practices&site=stackoverflow", gzip: true }, function(error, response, body) { console.log(body); // I'm still a string body = JSON.parse(body); // Now I'm a JSON object res.json(body); // converts and sends JSON }); }); 

你被这个callback绊倒了。 res.send将在结果发生之前触发。

 app.get('/sof',function(req,res){ var ark; request("https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&q=python%20best%20practices&site=stackoverflow", function(error, response, body) { ark = body; console.log(body); // res only when you get results //res.send(ark); res.json(body); // converts and sends JSON }); }); 

应该pipe用。

编辑:(包括这个具体问题的gzip通缩)

根据StackExchange 文档,他们的结果被送回gzip'd(有道理)。 你需要告诉是这样的要求。

 app.get('/sof', function(req, res){ request.get({ url: "https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&q=python%20best%20practices&site=stackoverflow", gzip: true }, function(error, response, body) { console.log(body); // I'm still a string body = JSON.parse(body); // Now I'm a JSON object res.json(body); // converts and sends JSON }); }