Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接口代理数据,数据被截断 会是什么原因? #31

Closed
lee-my opened this issue Mar 26, 2019 · 10 comments
Closed

接口代理数据,数据被截断 会是什么原因? #31

lee-my opened this issue Mar 26, 2019 · 10 comments
Labels

Comments

@lee-my
Copy link

lee-my commented Mar 26, 2019

Uploading WX20190326-172052@2x1111111.png…
以前是好使的,可以代理,但是最近突然出现接口数据被截断,返回不了数据

@lee-my
Copy link
Author

lee-my commented Mar 26, 2019

image
像这样 返回的不是一个完整的json

@JasonBoy
Copy link
Owner

直接调接口会没么

@lee-my
Copy link
Author

lee-my commented Mar 27, 2019

直接调接口是好的

@JasonBoy
Copy link
Owner

可能是stream被某种网络原因关了吧,你在proxy.js里再pipe给PassThrough试一下:

const {PassThrough} = require('stream');
//....
router.all('*', ctx => {
    ctx.body = apiProxy.proxyRequest(ctx).pipe(PassThrough());
  });

@lee-my
Copy link
Author

lee-my commented Mar 27, 2019

厉害啊!! 好了
这是什么原因啊?

@JasonBoy
Copy link
Owner

koajs/koa#612

@lee-my
Copy link
Author

lee-my commented Mar 27, 2019

抱歉 又出现一个问题
返回的接口数据,在network中中文显示乱码了 页面上显示正常

WX20190327-161551@2x2222222

@lee-my
Copy link
Author

lee-my commented Mar 27, 2019

就谷歌会出现这样 火狐是好的

@JasonBoy
Copy link
Owner

🤦‍♂️ 应该是PassThrough不会把header 回给浏览器,试试先set头,然后再pipe:

    const requestStream = proxy.proxyRequest(ctx);
    const pt = requestStream.pipe(PassThrough());
    await new Promise(resolve => {
      requestStream.on('response', response => {
        ctx.status = response.statusCode;
        ctx.set(response.headers);
        resolve();
      });
    });
    ctx.body = pt;

@lee-my
Copy link
Author

lee-my commented Mar 27, 2019

router.all('*', async ctx => {
const requestStream = apiProxy.proxyRequest(ctx);
const pt = requestStream.pipe(PassThrough());
await new Promise(resolve => {
requestStream.on('response', response => {
ctx.set('content-type', response.headers['content-type']);
resolve();
});
});
ctx.body = pt;
});

这样写好了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants