同构提取不适用于外部请求?

时间:2016-04-29 17:28:22

标签: javascript node.js express fetch

编辑:我实际上想了解为什么响应不包含我请求的数据,以及是否由于某些库缺失或我的fetchUrl变量的格式

您好我正在尝试使用同构fetch方法发出ajax请求,并且正在努力从外部API获取数据。

不应该做这项工作,如果不是,我错过了什么?

require('es6-promise').polyfill();
require('isomorphic-fetch');          

router.get('/weather', function(req, res){
          var fetchUrl = 'http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json';
          fetch(fetchUrl, {
            method: "GET"
          })
            .then(function(response){
              if (response.status >= 400) {
                throw new Error("Bad request response from server");
              }
              console.log(response);
              res.send(response);
            });

        });

我的回答如下:

  

{" URL":" http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json""状态":200,"状态文本":&#34 ; OK""头" {" _headers" {"服务器":["阿帕奇/ 2.2.15   (CentOS的)&#34],"存取控制允许来源":[" *&#34],"访问控制 - 允许的凭证" :"真&#34]," X-CREATIONTIME":[" 0.140&#34],"内容编码":[&#34 ; gzip的&#34],"最后修改":["星期二,   2016年5月3日10:47:33 GMT"]," content-type":[" application / json;   字符集= UTF-8&#34],"内容长度":[" 1043&#34],"变化":["接受编码&# 34],"到期":["星期二,   2016年5月3日10:48:58 GMT"],"缓存控制":[" max-age = 0,   no-cache"]," pragma":[" no-cache"]," date":["星期二,2016年5月3日10 :48:58   GMT&#34],"连接":["靠近&#34]}}" OK":真,"主体":{& #34; _opts":{}," _chunkSize":16384" _readableState" {" objectMode":假," highWaterMark&#34 ;:16384"缓冲器":[],"长度":0,"管道":空," pipesCount":0,& #34;使":空,"截止":假," endEmitted":假,"读":假,"同步&#34 ;:假," needReadable":真," emittedReadable":假," readableListening":假," defaultEncoding":" UTF8"" ranOut":假," awaitDrain":0," readingMore":假,"解码器":空,& #34;编码":空}"可读":真,"结构域":空," _events" {"端&#34 ;:[NULL,NULL]}," _eventsCount":7," _writableState" {" objectMode":假," highWaterMark&#34 ;: 16384" needDrain":假,"结束":假,"截止":假,"成品":假," decodeStrings":真,&#34 ; defaultEncoding":" UTF8""长度":1043,"写":真,"塞住":0,&# 34;同步":假," bufferProcessing":假," writelen":1043," bufferedRequest":空," lastBufferedRequest" :空," pendingcb":1,"预涂":假," errorEmitted":假}"可写":真,&# 34; allowHalfOpen":真," _transformState" {" needTransform":假,"转化":真," writechunk&#34 ;: {"类型":"缓冲液""数据":[31,139,8,0,0,0,0,0,0,3,165,86,91,143,218 ,56,20,126,158,249,21,86,164,149,90,9,114,5,2,72,85,133,102,52,218,213,118,232,106,41,157,125,139,60,142,19,44,146,56,107,59,195,208,138,255,190,199,137,67,194,165,157,74,203,11,248,251,206,205,159,15,199,190,253,126,139,144,37,168,44,121,33,169,53 ,71,245,250,133,10,201,120,97,205,45,215,246,172,129,134,20,21,185,228,201,138,138,23,70,192,208,218,40,85,206,29,103,183,219,217,187,170,136,169,72,5,135,111,155,240,220,217,81,1 72,54,84,56,184,100,78,236,212,174,246,70,229,89,19,42,1,182,130,148,109,54,194,139,152,41,200,167,17,15,144,195,237,205,1,190,6,55,22,169,132,160,133,138,248,179,132,196,88,213,69,129,215,205,141,197,114,156,210,118,81,137,172,43,137,65,64,105,239,94,171,180, 46,38,21,184,220,48,34,157,93,229,59,25,79,121,228,5,238,235,212,181,203,34,133,130,192,91,49,149,233,45,61,53,101,163,117,183,157,198,32,99,197,246,231,91,182,192,236,80,219,198,76,150,25,222,71,25,39, 39,245,38,85,166,107,92,225,2,61,8,92,16,38,9,31,160,187,69,147,129,48,181,63,103,27,70,42,172,116,113,173,101,189,142,10,156,215,32,206,88,194,69,193,176,9,3,245,40,161, 35,173,87,....

2 个答案:

答案 0 :(得分:4)

Response是一个复杂的对象,它不仅包含有效负载,还包含许多元数据,例如http状态和标题。

为了提取实际数据,您可能需要调用以下方法之一:

response.json() // if the response contains json data

response.text() // if there is a plain text

您可以进一步调查MDN

更新。考虑到两种方法都返回Promise,因此您可以将它们集成到promise链中:

require('es6-promise').polyfill();
require('isomorphic-fetch');

var express = require('express');
var app = express();

app.get('/weather', function(req, res){
  var fetchUrl = 'http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json';
  fetch(fetchUrl)
    .then(function(response){
      if (response.status >= 400) {
        throw new Error("Bad request response from server");
      }
      return response.json();
    })
    .then(function (json) {
      console.log(json);
      res.json(json);
    });
});

app.listen(8080);

答案 1 :(得分:1)

所以我想我已经弄清楚了我的问题。

我需要正确处理响应对象,但我不明白正确的方法。

我正在请求一个json对象但是不能使用response.json()来获取数据?

什么有效:

<select id="getUsers" parameterType="java.util.Map" fetchSize="1000" resultType="java.util.LinkedHashMap" >
           select * from users;
<select>

我甚至不明白为什么会这样。当然,响应对象只是一个我可以像其他任何一样钻进的对象吗?有什么区别?