使用https.request和选项时连接重置。

huangapple go评论134阅读模式
英文:

Connection Resets When Using https.request with Options in NodeJS

问题

以下是您要翻译的内容:

在 NodeJS 中,当我使用 https.get() 时,我的代码正常工作,返回所需的响应。但一旦我切换到带有 requestOptions 的 https.request(),我会收到连接重置错误,带有以下堆栈跟踪:

err: {
  code: "ECONNRESET",
  stack: "Error: read ECONNRESET\n    at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)\n    at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17)"
}

我的代码如下。只需将 https.gethttps.request 互换:

const express = require('express');
const axios = require('axios');
const https = require('https');
const app = express();

app.all('/api/*', (req, res) => {
  forwardRequest(req, res);
});

app.listen(4600, () => {
  console.log('代理服务器监听端口 4600');
});

function forwardRequest(req, res) {
  const agolUrl = buildTargetUrl(req);

  const requestOptions = {
    port: 443,
    method: req.method,
    headers: req.headers,
  };

  // const proxyReq = https.get(agolUrl, (proxyRes) => {
  const proxyReq = https.request(agolUrl, requestOptions, (proxyRes) => {
    let responseData = '';
    proxyRes.on('data', (chunk) => {
      responseData += chunk;
    });
    proxyRes.on('end', async () => {
      doSomethingWithResponseData(responseData);
      res.writeHead(proxyRes.statusCode, proxyRes.headers);
      res.write(responseData);
      res.end();
    });

  }).on('error', (err) => {
    console.error(err);
    res.status(500).send('代理出现错误。');
  });

  req.pipe(proxyReq); // 这是做什么的?
}

function buildTargetUrl(req) {
  let targetUrl = 'https://some-remote-server.com' + req.originalUrl;

  targetUrl = decodeURI(targetUrl);

  targetUrl = targetUrl.replace('/api', '');
  
  return targetUrl;
}

function doSomethingWithResponseData(responseData) {
  // 自定义逻辑在这里。
}

依赖项如下:

// Node 版本为 v16.20.0
"dependencies": {
  "axios": "^1.4.0",
  "express": "^4.18.2",
  "request": "^2.88.2"
}

请注意,这与此问题相同,但不幸的是它尚未得到答案,因此在这里发布了一个新问题。

英文:

In NodeJS, when I use https.get(), my code works correctly, returning the required responses. But as soon as I switch over to https.request() with requestOptions, I get connection reset error, with the following stack trace:

err: {
code: "ECONNRESET",
stack: ""Error: read ECONNRESET\n    at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)\n    at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17)"
}

My code is below. Simply swap out the https.get & https.request:

const express = require('express');
const axios = require('axios');
const https = require('https');
const app = express();
app.all('/api/*', (req, res) => {
forwardRequest(req, res);
});
app.listen(4600, () => {
console.log('Proxy server listening on port 4600');
});
function forwardRequest(req, res) {
const agolUrl = buildTargetUrl(req);
const requestOptions = {
port: 443,
method: req.method,
headers: req.headers,
};
// const proxyReq = https.get(agolUrl, (proxyRes) => {
const proxyReq = https.request(agolUrl, requestOptions, (proxyRes) => {
let responseData = '';
proxyRes.on('data', (chunk) => {
responseData += chunk;
});
proxyRes.on('end', async () => {
doSomethingWithResponseData(responseData);
res.writeHead(proxyRes.statusCode, proxyRes.headers);
res.write(responseData);
res.end();
});
}).on('error', (err) => {
console.error(err);
res.status(500).send('An error occurred with proxy.');
});
req.pipe(proxyReq); // What does this do?
}
function buildTargetUrl(req) {
let targetUrl = 'https://some-remote-server.com' + req.originalUrl;
targetUrl = decodeURI(targetUrl);
targetUrl = targetUrl.replace('/api', '');
return targetUrl;
}
function doSomethingWithResponseData(responseData) {
// Custom logic here.
}

The dependencies are:

// Node version is v16.20.0
"dependencies": {
"axios": "^1.4.0",
"express": "^4.18.2",
"request": "^2.88.2"
}

Note, this is the same as this question but unfortunately it doesn't have an answer yet, so posting a new one here.

答案1

得分: 0

已解决。事实证明,在标头中,目标主机"https://some-remote-server.com"不喜欢host的值(在我的情况下,它是localhost)。

从标头中删除host有效。

英文:

OK, figured it out. It turns out in the headers, the target host "https://some-remote-server.com" does not like the host value (in my case, it was localhost).

Deleting the host from the header worked.

huangapple
  • 本文由 发表于 2023年5月11日 15:28:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定