xiongwilee / Gracejs

A Nodejs BFF framework, build with koa2(基于koa2的标准前后端分离框架)
https://grace.wilee.me
MIT License
1.39k stars 238 forks source link

this.proxy 丢失headers内容 #26

Closed qppq54s closed 7 years ago

qppq54s commented 7 years ago

在使用 this.proxy('http://jsonplaceholder.typicode.com/users?_page=1') 时,发现直接调用中返回的

headers: {
Access-Control-Allow-Credentials:true
Access-Control-Expose-Headers:X-Total-Count, Link
Cache-Control:public, max-age=14400
CF-Cache-Status:REVALIDATED
CF-RAY:33305cd0d6872246-LAX
Connection:keep-alive
Date:Sat, 18 Feb 2017 09:13:21 GMT
Etag:W/"160d-MxiAGkI3ZBrjm0xiEDfwqw"
Expires:Sat, 18 Feb 2017 13:13:21 GMT
Link:
Pragma:no-cache
Server:cloudflare-nginx
Vary:Accept-Encoding
Via:1.1 vegur
X-Content-Type-Options:nosniff
X-Powered-By:Express
X-Total-Count:10
}

而 this.proxy 的返回只有这些

headers: {
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Sat, 18 Feb 2017 09:05:13 GMT
set-cookie:__cfduid=d8215742543560aabf718ebc4a66c415f1487408713; expires=Sun, 18-Feb-18 09:05:13 GMT; path=/; domain=.typicode.com; HttpOnly
Transfer-Encoding:chunked
Vary:Accept-Encoding
}

请问该怎么解决这个问题

xiongwilee commented 7 years ago

我这儿没有问题:

exports.test = function*(){
    this.body = yield this.proxy({test:'http://jsonplaceholder.typicode.com/users?_page=1'})
}

返回:

{
...
"headers": {
"date": "Sat, 18 Feb 2017 12:08:39 GMT",
"content-type": "application/json; charset=utf-8",
"transfer-encoding": "chunked",
"connection": "keep-alive",
"set-cookie": [
"__cfduid=dbcee87783db1b6e65be02b998ab2d43f1487419719; expires=Sun, 18-Feb-18 12:08:39 GMT; path=/; domain=.typicode.com; HttpOnly"
],
"x-powered-by": "Express",
"vary": "Accept-Encoding",
"access-control-allow-credentials": "true",
"cache-control": "public, max-age=14400",
"pragma": "no-cache",
"expires": "Sat, 18 Feb 2017 16:08:39 GMT",
"x-total-count": "10",
"access-control-expose-headers": "X-Total-Count, Link",
"link": "",
"x-content-type-options": "nosniff",
"etag": "W/\"160d-MxiAGkI3ZBrjm0xiEDfwqw\"",
"content-encoding": "gzip",
"via": "1.1 vegur",
"cf-cache-status": "EXPIRED",
"server": "cloudflare-nginx",
"cf-ray": "33315d9c91166d00-SJC"
},
...
xiongwilee commented 7 years ago

我这儿没有问题:

exports.test = function*(){
    this.body = yield this.proxy({test:'http://jsonplaceholder.typicode.com/users?_page=1'})
}

返回:

{
...
"headers": {
"date": "Sat, 18 Feb 2017 12:08:39 GMT",
"content-type": "application/json; charset=utf-8",
"transfer-encoding": "chunked",
"connection": "keep-alive",
"set-cookie": [
"__cfduid=dbcee87783db1b6e65be02b998ab2d43f1487419719; expires=Sun, 18-Feb-18 12:08:39 GMT; path=/; domain=.typicode.com; HttpOnly"
],
"x-powered-by": "Express",
"vary": "Accept-Encoding",
"access-control-allow-credentials": "true",
"cache-control": "public, max-age=14400",
"pragma": "no-cache",
"expires": "Sat, 18 Feb 2017 16:08:39 GMT",
"x-total-count": "10",
"access-control-expose-headers": "X-Total-Count, Link",
"link": "",
"x-content-type-options": "nosniff",
"etag": "W/\"160d-MxiAGkI3ZBrjm0xiEDfwqw\"",
"content-encoding": "gzip",
"via": "1.1 vegur",
"cf-cache-status": "EXPIRED",
"server": "cloudflare-nginx",
"cf-ray": "33315d9c91166d00-SJC"
},
...
xiongwilee commented 7 years ago

估计你是想把其他的headers也返回给客户端吧?这种情况下建议最好别这么干~

qppq54s commented 7 years ago

那我想要把其中的一个比如“x-total-count”返回,该怎么做呢 为什么不把所有的headers返回?

xiongwilee commented 7 years ago

目前头信息里只会解析set-cookies字段(即返回的cookie),不返回所有的headers有几方面的考虑: 1、数据就是数据,头信息就是头信息,所有的数据通过response body体传递,避免混用; 2、如果有多个并发请求的话,以哪个请求的头信息为准呢?

qppq54s commented 7 years ago

恩,明白了,谢谢 请问一下我怎么附加信息到返回的headers中呢,我试了conf:{headers:{}} 没有作用