【问题标题】:Returning Json with nodeJS使用 nodeJS 返回 Json
【发布时间】:2016-06-06 16:16:44
【问题描述】:

我尝试使用 Clash of clan API 来学习 nodeJS 和 AngularJS,这里提供 https://developer.clashofclans.com/#/

我的 JSON api 的返回被截断,我不知道如何获得完整的响应。

我的 app.js 是用 node 和 express 写的:

var   express       = require('express')
, https         = require('https')
, bodyParser    = require('body-parser')
, request       = require('request')
, app           = express()
, http_port     = 3000;

app.use(express.static(__dirname + '/public'));
app.use(bodyParser());

var options = {
    host: 'api.clashofclans.com',
    port: 443,
    path: '/v1/clans/' + encodeURIComponent("#PP8JC9RQ"),
    headers : {
    accept : "application/json",
    authorization : "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6ImQ4Njg1OGFhLWQzZTUtNDNiOC05MTM1LTBjNzI1ZjI4OGFiMiIsImlhdCI6MTQ1NjMwMTE5MSwic3ViIjoiZGV2ZWxvcGVyLzk1YWM0YjdmLTY5NTQtMDE3MC01ZjAyLTcxNjY1ZDMzYjUwNyIsInNjb3BlcyI6WyJjbGFzaCJdLCJsaW1pdHMiOlt7InRpZXIiOiJkZXZlbG9wZXIvc2lsdmVyIiwidHlwZSI6InRocm90dGxpbmcifSx7ImNpZHJzIjpbIjg3LjIzMS4yMTMuMTU0Il0sInR5cGUiOiJjbGllbnQifV19.SYqeSAF-0_bM1eS2-hnovvj5j-I0SQpdr_kySIiBKw9OkrNuBzZAOAOkiH3fzdKSkcHaJfXzWdXr8JozFfAmJA"
},
method: 'GET'
};

//  Route par défaut
    app.get("/members", function(req, res) {
    console.log("-------------------------------------\n\nroot path !! Let's get clan infos from coc api\n\n-------------------------------------");
    var req = https.request(options, function(response){

        response.on('data', function (chunk) {          
            console.log('BODY: ' + chunk);
            //    in console JSON is full but in my html JSON is truncated..
            res.send(chunk);
        });         
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    req.end();

    });


app.listen(http_port,function(){
    console.log("Listening on " + http_port);
});

如果有人可以教到一个角落:)

【问题讨论】:

    标签: javascript angularjs json node.js


    【解决方案1】:

    您的代码正在将响应的第一块立即发送回用户。您需要等到整个 HTTP 响应完成。试试这样的:

    app.get("/members", function(req, res) {
        var req = https.request(options, function(response){
        var httpResult = '';
        response.on('data', function (chunk) {          
            console.log('BODY: ' + chunk);
            httpResult += chunk;
        });         
        response.on('end', function() {
           res.send(httpResult);
        });
    });
    
    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });
    
    req.end();
    
    });
    

    查看每次调用数据事件以及完成时如何附加到 var - 然后我发送输出。

    【讨论】:

    • 在我看来,我尝试了这个解决方案但没有成功。按预期工作非常好!
    【解决方案2】:

    因为您使用send 方法而不是write(发送非流式传输)。试试这个:

    app.get("/members", function(req, res) {
    
        https.request(options, function(response){
    
            response.on('data', function (chunk) {          
                res.write(chunk);
            });         
    
            response.on('end', function () {          
                res.end();
            });         
    
        }).end();
    
    });
    

    【讨论】:

      【解决方案3】:

      response 是一个流,您可以简单地将其通过管道传递给res

      app.get("/members", function(req, res) {
        https.request(options, function(response) {
          response.pipe(res);
        });
      });
      

      【讨论】:

      • 我需要把response.pipe(res)放在response.on('data', function (chunk) {});里面吗??
      • @akio 不,一行就足够了。 pipe 为您处理“数据”事件,并在需要时执行所有需要的 read()write()。数据在 responseres 之间很好地流动,就像pipes 中的水流一样。
      猜你喜欢
      • 2016-04-16
      • 2015-06-27
      • 2017-09-26
      • 2017-09-09
      • 2013-10-05
      • 1970-01-01
      • 2019-08-11
      • 2019-04-28
      • 2015-05-21
      相关资源
      最近更新 更多