使用request.js代理post失败的问题

 

前面写过一篇使用request.js作代理的文章,可能眼睛敏锐的朋友已经看出在代理POST方法时和代理其它请求方式是有区别的, 如今我来讲一下为何要这么处理。html

 

相信不少人都采用这种方式去代理POST方法,可是你有可能代理不过去,请求会被挂起!git

req.pipe(request({
    method: 'POST',
    uri: 'http://localhost:8080/api'
})).pipe(res);

为何呢?github

由于你可能使用了body-parse中间件api

require('body-parser').urlencoded({extended: false})

 

解决方案:post

  1)删除urlencoded中间件ui

  2)改用以下方式代理(取出body后从新组装发送)url

request.post({
     uri: 'http://localhost:8080/api',
     form: req.body
}).pipe(res);

 

参考文献:spa

  https://github.com/request/request/issues/1664代理

相关文章
相关标签/搜索