使用Node.js的代理请求:net :: ERR_EMPTY_RESPONSE

最终,我只是试图从浏览器张贴图像到服务器。 不幸的是,遇到CORS问题,所以暂时尝试使用我们的Node.js服务器作为代理服务器。

我有这个:

router.post('/image', function (req, res, next) { const filename = uuid.v4(); const proxy = http.request({ method: 'PUT', hostname: 'engci-maven.nabisco.com', path: `/artifactory/cdt-repo/folder/${filename}`, headers: { 'Authorization': 'Basic ' + Buffer.from('foo:bar').toString('base64'), } }); req.pipe(proxy).pipe(res).once('error', next); }); 

浏览器启动请求,但是我在浏览器中得到一个错误,说我得到一个空的响应,错误是:

在这里输入图像说明

有谁知道为什么会出现这个错误? Node.js中的代理代码有问题吗? 授权应该没问题,而且请求url应该没问题。 不知道发生了什么事。

好吧,所以这个工作,但我不知道为什么:

 router.post('/image', function (req, res, next) { const filename = uuid.v4(); const proxy = http.request({ method: 'PUT', hostname: 'engci-maven.nabisco.com', path: `/artifactory/cdt-repo/folder/${filename}`, headers: { 'Authorization': 'Basic ' + Buffer.from('foo:bar').toString('base64'), } }, function(resp){ resp.pipe(res).once('error', next); }); req.pipe(proxy).once('error', next); }); 

有一个解释为什么这个Node.js的帮助线程工作: https : //github.com/nodejs/help/issues/760