node.js - 在没有代理的本地域上修复 Axios 错误 ECONNREFUSED
问题描述
我在使用 Axios 时遇到了这个愚蠢的问题。
我正在尝试使用以下代码向我的基本本地 Symfony 应用程序发送本地 POST 请求,这是一个噩梦 JS 爬虫:Yuzu.fun 是我的本地开发域,在 /etc/hosts 中有一个条目来解决它,应用程序在端口 80 上运行。
await axios.request({
url: 'http://yuzu.fun/app_dev.php/api/path/to/endpoint',
headers: {
'key': 'xxxxxx'
},
method: 'post',
proxy: false,
data: {
image_data: data
}
})
.then(function (response) {
map_hash = response.message;
console.log('buildMap', map_hash);
})
.catch(function (error) {
console.error('upload failed:', error);
});
当我运行我的脚本时,这是我得到的错误:
upload failed: { Error: connect ECONNREFUSED 127.0.0.1:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
key: 'xxxxx',
'User-Agent': 'axios/0.18.0',
'Content-Length': 4173 },
method: 'post',
url: 'http://yuzu.fun/app_dev.php/api/path/to/endpoint',
proxy: false,
data: '{"image_data":"imagedatainbase64"}' },
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,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
domain: null,
_events:
{ response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'http:',
path: '/app_dev.php/api/path/to/endpoint',
method: 'post',
headers: [Object],
agent: undefined,
auth: undefined,
hostname: 'yuzu.fun',
port: null,
nativeProtocols: [Object],
pathname: '/app_dev.php/api/path/to/endpoint' },
_ended: false,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 4173,
_requestBodyBuffers: [ [Object] ],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'POST /app_dev.php/api/path/to/endpoint HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/json;charset=utf-8\r\nkey: xxxxx\r\nUser-Agent: axios/0.18.0\r\nContent-Length: 4173\r\nHost: yuzu.fun\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/app_dev.php/api/path/to/endpoint',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(outHeadersKey)]: [Object] },
_currentUrl: 'http://yuzu.fun//app_dev.php/api/path/to/endpoint' },
response: undefined }
我尝试发布到一个随机端点以检查连接在到达实时域时是否正常工作,这没关系,但它在本地不起作用。当我检查 stackoverflow 或论坛时,我看到人们提到代理问题,所以我强迫它为假,但我不认为这是我关心的事情。
这段代码是首先编写的,request.post()
但我需要有 Axios Promises 的好处。使用请求编写代码时,可以正确访问本地域。所以我排除了噩梦可能会干扰域解析的事实。
有没有人知道这里可能会出错?
提前致谢!
解决方案
推荐阅读
- algorithm - 伪对角化邻接矩阵
- python - ValueError:将字典与非系列混合可能会导致排序不明确
- javascript - 如何调试“TypeError:承诺无法自行解决”
- android - EditTextPreference does not mask password even with android:inputType="textPassword"
- python - 在 python 中,我们可以通过什么逻辑添加“for”循环和另一个语句,就像在提到的示例中所做的那样?
- java - Spring Boot @Query Annotation countQuery 有效,但 countName 无效
- python - 有 matplotlib.pyplot.colors 的替代品吗?
- python - 如何限制 Django 中忘记密码重置尝试的次数?
- kubernetes - kubernetes v1.15.0 master 无法访问 pod ip 地址
- c# - 再次在接口中构造;为什么我可以像上课一样使用它?