AXIOS请求:“错误:连接ECONNREFUSED 127.0.0.1:80”

e5nszbig  于 2023-06-22  发布在  iOS
关注(0)|答案(5)|浏览(709)

试图理解我的GET请求发生了什么。我使用node.js在后端设置了这个。我第一次以为我使用了错误的.env,url,用户名和密码...但是当我检查Postman时,Basic Auth似乎可以毫无问题地获取JSON数据。我是不是用错AXIOS了?
下面是我的GET请求:

let customers;
try {
    let user_res = await axios({
        method: "GET",
        url: "api._____.com/customers",
        auth: {
            username: process.env.USERNAME,
            password: process.env.PASSWORD
        }
    });
    customers = user_res.data;
    console.log(customers);
} catch (err) {
    const error = new HttpError(
        "API failed, please try again.",
        500
    );
    return next(error);
}

错误日志:

API_NAME error Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
    errno: -61,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 80,
    config: {
        url: 'api.___.com/customers',
        method: 'get',
        headers: {
        Accept: 'application/json, text/plain, */*',
        'User-Agent': 'axios/0.19.1'
        },
        auth: {
        username: '___',
        password: '___'
        },
        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: <ref *1> 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,
        errored: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object]
        },
        writable: true,
        _events: [Object: null prototype] {
        response: [Function: handleResponse],
        error: [Function: handleRequestError]
        },
        _eventsCount: 2,
        _maxListeners: undefined,
        _options: {
        protocol: 'http:',
        maxRedirects: 21,
        maxBodyLength: 10485760,
        path: 'api.___.com/customers',
        method: 'GET',
        headers: [Object],
        agent: undefined,
        agents: [Object],
        auth: '___',
        hostname: null,
        port: null,
        nativeProtocols: [Object],
        pathname: 'api.___.com/customers'
        },
        _redirectCount: 0,
        _redirects: [],
        _requestBodyLength: 0,
        _requestBodyBuffers: [],
        _onNativeResponse: [Function (anonymous)],
        _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: [Socket],
        _header: 'GET api.___.com/customers HTTP/1.1\r\n' +
            'Accept: application/json, text/plain, */*\r\n' +
            'User-Agent: axios/0.19.1\r\n' +
            'Host: localhost\r\n' +
            'Authorization: Basic ___==\r\n' +
            'Connection: close\r\n' +
            '\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Agent],
        socketPath: undefined,
        method: 'GET',
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        path: 'api.___.com/customers',
        _ended: false,
        res: null,
        aborted: false,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        reusedSocket: false,
        _redirectable: [Circular *1],
        [Symbol(kCapture)]: false,
        [Symbol(kNeedDrain)]: false,
        [Symbol(corked)]: 0,
        [Symbol(kOutHeaders)]: [Object: null prototype]
        },
        _currentUrl: 'http:api.___.com/customers',
        [Symbol(kCapture)]: false
    },
    response: undefined,
    isAxiosError: true,
    toJSON: [Function (anonymous)]
}
z8dt9xmd

z8dt9xmd1#

我知道你的URL是这样定义的:api.___.com/customers,但是如果有的话,应该像http://api.___.com/customershttps://api.___.com/customers一样定义它。

jm2pwxwz

jm2pwxwz2#

那么,127.0.0.1意味着您的本地计算机(127.0.0.1)在端口80上拒绝了它。也许你需要在你的本地机器上给那个端口添加一个例外?另一件要注意的事情是CORS问题,如果你的服务器本身,但通常会遇到一个错误,详细说明了这种情况。

xfyts7mz

xfyts7mz3#

对于我的情况,我找到了一个解决方案:我使用localhost:8000部署我的API项目,由于我的host.conf文件,听起来localhost不是直接的127.0.0.1。所以我在127.0.0.1:8000上部署我的lumen应用程序,然后我的expressJs连接工作

1szpjjfi

1szpjjfi4#

我用HTTP设置了URL,问题就解决了!

axios.post('http://localhost:8080/user.json', {
    firstName: 'Fred',
    lastName: 'Flintstone'
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});
knsnq2tg

knsnq2tg5#

Nuxt 3用户在这里。对我来说,这是因为我设置了网站Map创建之前所有的插件等等。已加载;所以axios.defaults.baseurl导致了一个错误。我将变量更改为https://api.blahblah.com,错误消失了。

相关问题