gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:18:53 25 4
gpt4 key购买 nike

最终,我只是想将图像从浏览器发布到服务器。不幸的是遇到了 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);

});

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

enter image description here

有谁知道为什么会出现这个错误?我在 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

关于javascript - 使用 Node.js 的代理请求:net::ERR_EMPTY_RESPONSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com