pipe request 无效的问题~
发布于 7 年前 作者 Sunshine168 5056 次浏览 来自 问答

在改写一个koa-proxy 需要将转发的pipe到request中,但是没效 写法如下,使用的是 request

         //req是一个原生request来自ctx.req
                req.pipe(request(options))
                    .on('err', (error) => {
                        console.log(error)
                    })
                    .on('response', (res) => {
                        console.log(res.body)//undefined ,接受端的路由直接返回请求,通过打印判断是否转发成功
                    })

参考原本的写法

var coRequest = require('co-request');
var requestThunk = request(opt);
var res = yield pipeRequest(this.req, requestThunk);
function pipeRequest(readable, requestThunk) {
  return function(cb) {
    readable.pipe(requestThunk(cb));
  }
}

发送的数据是以post形式发送的json

{"foo": "bar"}

由于需要兼顾不使用koa-parsebody的情况,就是不能直接通过ctx.request.body 要转发ctx.req 达到 发送数据的效果~~ 有没有其他思路求助呀~ 还是我的想法有问题~ 求指出。。

8 回复

没太看明白,proxy就proxy为啥要和koa混在一起。。。http不够么?

我看楼主的意思是不想通过application/x-www-form-urlencoded,而是通过multipart/form-data来传输。post 提交数据时候设置 enctype 为后者即可

@i5ting https://github.com/popomore/koa-proxy 就是这个中间件啦 想改写成koa2 中间件 async的形式~ 来进行学习 orz

@DevinXian orz 是在改写这个https://github.com/popomore/koa-proxy 到2.0的中间件~ 跟那个什么关系啦

@Sunshine168 不是有 koa-convert

@DevinXian 对啊 我知道,但是作为一种学习啦 在改写中间件的时候学习一下人家怎么写的和加深对koa的了解

@Sunshine168 这个态度是非常好的!严重支持!

@DevinXian 谢谢老哥哈哈 哎 人不聪明只能多下点功夫啦

回到顶部