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

编辑:我实际上希望了解为什么响应不包含我请求的数据,是否由于某些库丢失或我的fetchUrlvariables的格式

您好我试图使用同构的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 ”,“status”:200,“statusText”:“确定”,“标题” :{“_ headers”:{“server”:[“Apache / 2.2.15(CentOS)”],“access-control-allow-origin”:[“*”],“access-control-allow-credentials” [“true”],“x-creationtime”:[“0.140”],“content-encoding”:[“gzip”],“last-modified”:[“Tue,03 May 2016 10:47:33 GMT” ]“content-type”:[“application / json; charset = UTF-8”],“content-length”:[“1043”],“vary”:[“Accept-Encoding”],“expires”: [“2016年5月3日星期二10:48:58 GMT”],“cache-control”:[“max-age = 0,no-cache”],“pragma”:[“no-cache”], “:[”2016年5月3日星期二10:48:58 GMT“],”连接“:[”close“]},”ok“:true,”body“:{”_ opts“:{},_ _ chunkSize “:16384,” _ readableState “:{” objectMode “:假” highWaterMark “:16384,” 缓冲液 “:[],” 长度 “:0,” pipe道 “:NULL,” pipesCount “:0,” stream“:空, “结束”:假的, “endEmitted”:虚假的, “读书”:假的, “同步”:假的, “needReadable”:真实的, “emittedReadable”:假的, “readableListening”:假的, “defaultEncoding”:“UTF8 “ ”ranOut“:假的, ”awaitDrain“:0,” readingMore “:假,” 解码器 “:空,” 编码 “:空},” 可读的 “:真,” 域 “:空,” _事件 “:{” 端 “:[NULL,NULL]},” _ eventsCount“: 7, “_ writableState”:{ “objectMode”:假的, “highWaterMark”:16384, “needDrain”:假的, “结束”:假的, “结束”:假的, “已完成”:假的, “decodeStrings”:真” defaultEncoding “:” UTF8" , “长度”:1043, “书写”:真实的, “木塞味”:0, “同步”:假的, “bufferProcessing”:假的, “writelen”:1043, “bufferedRequest”:空” lastBufferedRequest “:空,” pendingcb “:1,” 预涂 “:假的,” errorEmitted “:假},” 写 “:真实的,” allowHalfOpen “:真实的,” _ transformState “:{” needTransform “:假的,” 转化” :真 “writechunk”:{ “types”: “缓冲器”, “数据”:[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,172,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,….

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); 

所以我想我已经知道了我的问题。

我需要正确处理响应对象,但我不明白正确的方法来做到这一点。

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

什么工作:

  ... return response.text(); }).then(function(body) { res.send(body); ... 

我甚至不明白这是为什么。 响应对象当然只是一个可以像其他任何东西一样钻入的对象? 有什么不同?