gpt4 book ai didi

node.js - NodeJs ECONNRESET

转载 作者:搜寻专家 更新时间:2023-10-31 22:21:01 25 4
gpt4 key购买 nike

我在执行 GET 请求时遇到错误。

Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }

当我尝试向 https://www.adidas.co.uk/api/search/taxonomy?query=men 发出 GET 请求时会发生这种情况,但当我使用 时它会起作用https://jsonplaceholder.typicode.com/api/todos/1.

要求:

var request = require('request');
[...]
app.use('/api', cacheMiddleware(), function (req, out) {


var headers = {
//'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0',
'Content-Type': 'application/x-www-form-urlencoded'
//'Content-Type': 'application/jsond'
};

var url = getDestinationUrl(req);
if (req.method === 'POST') {
console.log('POST body:')
console.log(req.body);
result = request.post({ uri: url, json: req.body, headers: headers, followRedirect: true , maxRedirects: 10}, function (error, response, body) { handleResponse(req, out, error, response, body) });
} else {
result = request({ uri: url, headers: headers, followRedirect: true, maxRedirects: 10 }, function (error, response, body) { handleResponse(req, out, error, response, body) });
}

});

日志:

    handle https://www.adidas.co.uk/api/search/taxonomy?query=men
REQUEST { uri: 'https://www.adidas.co.uk/api/search/taxonomy?query=men',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
followRedirect: true,
maxRedirects: 10,
callback: [Function] }
HTTP 10384: SERVER socketOnParserExecute 739
REQUEST make request https://www.adidas.co.uk/api/search/taxonomy?query=men
HTTP 10384: call onSocket 0 0
HTTP 10384: createConnection www.adidas.co.uk:443:::::::::::::::: { servername: 'www.adidas.co.uk',
_defaultAgent:
Agent {
_events: [Object: null prototype] { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: { path: null },
requests: {},
sockets: { 'www.adidas.co.uk:443::::::::::::::::': [] },
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: { map: {}, list: [] } },
_events:
[Object: null prototype] {
error: [Function: bound ],
complete: [Function: bound ],
pipe: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
uri:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.adidas.co.uk',
port: 443,
hostname: 'www.adidas.co.uk',
hash: null,
search: '?query=men',
query: 'query=men',
pathname: '/api/search/taxonomy',
path: '/api/search/taxonomy?query=men',
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
headers:
{ 'Content-Type': 'application/x-www-form-urlencoded',
host: 'www.adidas.co.uk' },
followRedirect: true,
maxRedirects: 10,
callback: [Function],
readable: true,
writable: true,
_qs:
Querystring {
request:
Request {
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
uri: [Url],
headers: [Object],
followRedirect: true,
maxRedirects: 10,
callback: [Function],
readable: true,
writable: true,
_qs: [Circular],
_auth: [Auth],
_oauth: [OAuth],
_multipart: [Multipart],
_redirect: [Redirect],
_tunnel: [Tunnel],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.adidas.co.uk',
path: '/api/search/taxonomy?query=men',
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
lib:
{ formats: [Object], parse: [Function], stringify: [Function] },
useQuerystring: undefined,
parseOptions: {},
stringifyOptions: {} },
_auth:
Auth {
request:
Request {
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
uri: [Url],
headers: [Object],
followRedirect: true,
maxRedirects: 10,
callback: [Function],
readable: true,
writable: true,
_qs: [Querystring],
_auth: [Circular],
_oauth: [OAuth],
_multipart: [Multipart],
_redirect: [Redirect],
_tunnel: [Tunnel],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.adidas.co.uk',
path: '/api/search/taxonomy?query=men',
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
hasAuth: false,
sentAuth: false,
bearerToken: null,
user: null,
pass: null },
_oauth:
OAuth {
request:
Request {
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
uri: [Url],
headers: [Object],
followRedirect: true,
maxRedirects: 10,
callback: [Function],
readable: true,
writable: true,
_qs: [Querystring],
_auth: [Auth],
_oauth: [Circular],
_multipart: [Multipart],
_redirect: [Redirect],
_tunnel: [Tunnel],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.adidas.co.uk',
path: '/api/search/taxonomy?query=men',
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
params: null },
_multipart:
Multipart {
request:
Request {
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
uri: [Url],
headers: [Object],
followRedirect: true,
maxRedirects: 10,
callback: [Function],
readable: true,
writable: true,
_qs: [Querystring],
_auth: [Auth],
_oauth: [OAuth],
_multipart: [Circular],
_redirect: [Redirect],
_tunnel: [Tunnel],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.adidas.co.uk',
path: '/api/search/taxonomy?query=men',
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
boundary: '54c4df7c-a16d-45e6-a57b-bb7f83e6b1fa',
chunked: false,
body: null },
_redirect:
Redirect {
request:
Request {
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
uri: [Url],
headers: [Object],
followRedirect: true,
maxRedirects: 10,
callback: [Function],
readable: true,
writable: true,
_qs: [Querystring],
_auth: [Auth],
_oauth: [OAuth],
_multipart: [Multipart],
_redirect: [Circular],
_tunnel: [Tunnel],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.adidas.co.uk',
path: '/api/search/taxonomy?query=men',
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
followRedirect: true,
followRedirects: true,
followAllRedirects: false,
followOriginalHttpMethod: false,
allowRedirect: [Function],
maxRedirects: 10,
redirects: [],
redirectsFollowed: 0,
removeRefererHeader: false },
_tunnel:
Tunnel {
request:
Request {
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
uri: [Url],
headers: [Object],
followRedirect: true,
maxRedirects: 10,
callback: [Function],
readable: true,
writable: true,
_qs: [Querystring],
_auth: [Auth],
_oauth: [OAuth],
_multipart: [Multipart],
_redirect: [Redirect],
_tunnel: [Circular],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.adidas.co.uk',
path: '/api/search/taxonomy?query=men',
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men' },
proxyHeaderWhiteList:
[ 'accept',
'accept-charset',
'accept-encoding',
'accept-language',
'accept-ranges',
'cache-control',
'content-encoding',
'content-language',
'content-location',
'content-md5',
'content-range',
'content-type',
'connection',
'date',
'expect',
'max-forwards',
'pragma',
'referer',
'te',
'user-agent',
'via' ],
proxyHeaderExclusiveList: [] },
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'www.adidas.co.uk',
path: null,
httpModule:
{ Agent: { [Function: Agent] super_: [Function] },
globalAgent:
Agent {
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
Server: { [Function: Server] super_: [Function] },
createServer: [Function: createServer],
get: [Function: get],
request: [Function: request] },
agentClass:
{ [Function: Agent]
super_:
{ [Function: Agent] super_: [Function], defaultMaxSockets: Infinity } },
agent:
Agent {
_events: [Object: null prototype] { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: { path: null },
requests: {},
sockets: { 'www.adidas.co.uk:443::::::::::::::::': [] },
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: { map: {}, list: [] } },
_started: true,
href: 'https://www.adidas.co.uk/api/search/taxonomy?query=men',
_agentKey: 'www.adidas.co.uk:443::::::::::::::::' }
HTTP 10384: sockets www.adidas.co.uk:443:::::::::::::::: 1
HTTP 10384: outgoing message end.
HTTP 10384: SOCKET ERROR: read ECONNRESET Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27)
handleResponse...
Response error
{ Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }
HTTP 10384: outgoing message end.
HTTP 10384: CLIENT socket onClose
HTTP 10384: removeSocket www.adidas.co.uk:443:::::::::::::::: writable: false
HTTP 10384: HTTP socket close
HTTP 10384: server socket close

更新:它适用于 axios:

app.use('/api', cacheMiddleware(), function (req, out) {
var url = getDestinationUrl(req);
if (req.method === 'POST') {
axios.post(url, req.body)
.then(response => {
handleResponse(req, out, response, response.data)
})
.catch(error => {
handleResponseError(req, out, error)
});
} else {
axios.get(url)
.then(response => {
handleResponse(req, out, response, response.data)
})
.catch(error => {
handleResponseError(req, out, error)
});
}
});

最佳答案

"ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection.

此错误仅表示另一方以可能不正常(或可能很匆忙)的方式关闭了连接。

一个例子是:一个套接字连接可能由于多种原因被对方突然关闭,或者您在运行您的应用程序时丢失了 wifi 信号。然后你会在你这边看到这个错误/异常。

解决方案- 发生这种情况是因为您没有监听/处理“错误”事件,要处理它,您应该放置一个可以处理此类错误的监听器。

  • 安全地“抛出”错误
  • 安全地“捕获”错误

它在 Axios 中工作,因为您正在使用 promises 进行异步错误处理。

关于node.js - NodeJs ECONNRESET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53912439/

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