I am using axios and cheerio to scrape a site viewable through another computer on the same network's localhost. It's normally accessible through this machine via https://<Machine on Network's IP>:<port>
However, I get an ECONNRESET error with not a lot of code to debug, and no leads as to what on either end of the connection could cause the problem.
Specific IP/Port info hidden for privacy
const axios = require('axios');
const cheerio = require('cheerio');
const url = 'https://XX.X.XX.XXX:####';
try {
axios.get(url)
.then(response => {
const $ = cheerio.load(response.data);
console.log(response.data)
})
.catch(error => {
console.log(error);
})
} catch (err) {
console.log(err);
}
Running the code gives the following error:
Error: Client network socket disconnected before secure TLS connection was established
at connResetException (internal/errors.js:609:14)
at TLSSocket.onConnectEnd (_tls_wrap.js:1536:19)
at Object.onceWrapper (events.js:421:28)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1221:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'ECONNRESET',
path: null,
host: 'XX.X.XX.XXX',
port: '####',
localAddress: undefined,
config: {
url: 'https://XX.X.XX.XXX:####',
method: 'get',
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.19.2'
},
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
data: undefined
},
request: Writable {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object]
},
writable: true,
_events: [Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError]
},
_eventsCount: 2,
_maxListeners: undefined,
_options: {
protocol: 'https:',
maxRedirects: 21,
maxBodyLength: 10485760,
path: '/',
method: 'GET',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: 'XX.X.XX.XXX',
port: '####',
nativeProtocols: [Object],
pathname: '/'
},
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [TLSSocket],
connection: [TLSSocket],
_header: 'GET / HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'User-Agent: axios/0.19.2\r\n' +
'Host: XX.X.XX.XXX:####\r\n' +
'Connection: close\r\n' +
'\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: 'GET',
insecureHTTPParser: undefined,
path: '/',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
_redirectable: [Circular],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
},
_currentUrl: 'https://XX.X.XX.XXX:####/',
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function]
}
Any guidance would be appreciated
Found the problem,
I used Dev Tools on Chrome to go to the Security Overview for the page I'm trying to access, the page was listed as having a "non-HTTPS secure origin"
Thus, I tried making the small change to my url (swapping http for https):
'http://XX.X.XX.XXX:####'
Got the response almost immediately
Simple, but glad I found it