AxiosError: connect ECONNREFUSED 127.0.0.1:80 with axios

Viewed 53

I'm trying to connect to API with axios.post but I get error: I try to connect to 'localhost:5000/api/name/user/validate' and it seems that the api is running well, but axios gives me error, my aplication listens on port 4000 and the api on 5000 so it maybe might be a problem? The error:

>     AxiosError: connect ECONNREFUSED 127.0.0.1:80
>         at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
>       port: 80,
>       address: '127.0.0.1',
>       syscall: 'connect',
>       code: 'ECONNREFUSED',
>       errno: -4078,
>       config: {
>         transitional: {
>           silentJSONParsing: true,
>           forcedJSONParsing: true,
>           clarifyTimeoutError: false
>         },
>         adapter: [Function: httpAdapter],
>         transformRequest: [ [Function: transformRequest] ],
>         transformResponse: [ [Function: transformResponse] ],
>         timeout: 0,
>         xsrfCookieName: 'XSRF-TOKEN',
>         xsrfHeaderName: 'X-XSRF-TOKEN',
>         maxContentLength: -1,
>         maxBodyLength: -1,
>         env: { FormData: [Function] },
>         validateStatus: [Function: validateStatus],
>         headers: {
>           Accept: 'application/json, text/plain, */*',
>           'Content-Type': 'application/json',
>           'User-Agent': 'axios/0.27.2',
>           'Content-Length': 225
>         },
>         method: 'post',
>         url: 'http:localhost:5000/api/name/user/validate',
>         data: '{"proxy":false,"headers":{"Authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MzA0YmYxZmVhM2RiNDYyYjNhM2FiM2IiLCJpYXQiOjE2NjEyODA2MzksImV4cCI6MTY2MTg4NTQzOX0.ClIS6Z3iYAM1631f59Pgu6Gy1MfZFwnqMEk_IXheAzg"}}'
> 
>       },
>       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,
>           buffered: [],
>           bufferedIndex: 0,
>           allBuffers: true,
>           allNoop: true,
>           pendingcb: 0,
>           constructed: true,
>           prefinished: false,
>           errorEmitted: false,
>           emitClose: true,
>           autoDestroy: true,
>           errored: null,
>           closed: false,
>           closeEmitted: false,
>           [Symbol(kOnFinished)]: []
>         },
>         _events: [Object: null prototype] {
>           response: [Function: handleResponse],
>           error: [Function: handleRequestError],
>           socket: [Function: handleRequestSocket]
>         },
>         _eventsCount: 3,
>         _maxListeners: undefined,
>         _options: {
>           maxRedirects: 21,
>           maxBodyLength: 10485760,
>           protocol: 'http:',
>           path: 'localhost:5000/api/name/user/validate',
>           method: 'POST',
>           headers: [Object],
>           agent: undefined,
>           agents: [Object],
>           auth: undefined,
>           hostname: null,
>           port: null,
>           nativeProtocols: [Object],
>           pathname: 'localhost:5000/api/name/user/validate'
>         },
>         _ended: false,
>         _ending: true,
>         _redirectCount: 0,
>         _redirects: [],
>         _requestBodyLength: 225,
>         _requestBodyBuffers: [ [Object] ],
>         _onNativeResponse: [Function (anonymous)],
>         _currentRequest: ClientRequest {
>           _events: [Object: null prototype],
>           _eventsCount: 7,
>           _maxListeners: undefined,
>           outputData: [],
>           outputSize: 0,
>           writable: true,
>           destroyed: false,
>           _last: true,
>           chunkedEncoding: false,
>           shouldKeepAlive: false,
>           maxRequestsOnConnectionReached: false,
>           _defaultKeepAlive: true,
>           useChunkedEncodingByDefault: true,
>           sendDate: false,
>           _removedConnection: false,
>           _removedContLen: false,
>           _removedTE: false,
>           _contentLength: null,
>           _hasBody: true,
>           _trailer: '',
>           finished: false,
>           _headerSent: true,
>           _closed: false,
>           socket: [Socket],
>           _header: 'POST localhost:5000/api/name/user/validate HTTP/1.1\r\n' +
>             'Accept: application/json, text/plain, */*\r\n' +
>             'Content-Type: application/json\r\n' +
>             'User-Agent: axios/0.27.2\r\n' +
>             'Content-Length: 225\r\n' +
>             'Host: localhost\r\n' +
>             'Connection: close\r\n' +
>             '\r\n',
>           _keepAliveTimeout: 0,
>           _onPendingData: [Function: nop],
>           agent: [Agent],
>           socketPath: undefined,
>           method: 'POST',
>           maxHeaderSize: undefined,
>           insecureHTTPParser: undefined,
>           path: 'localhost:5000/api/name/user/validate',
>           _ended: false,
>           res: null,
>           aborted: false,
>           timeoutCb: null,
>           upgradeOrConnect: false,
>           parser: null,
>           maxHeadersCount: null,
>           reusedSocket: false,
>           host: 'localhost',
>           protocol: 'http:',
>           _redirectable: [Circular *1],
>           [Symbol(kCapture)]: false,
>           [Symbol(kNeedDrain)]: false,
>           [Symbol(corked)]: 0,
>           [Symbol(kOutHeaders)]: [Object: null prototype]
>         },
>         _currentUrl: 'localhost:5000/api/name/user/validate',
>         [Symbol(kCapture)]: false
>       }
>     }

My code, I try to connect to "localhost:5000/api/name/user/validate" and pass authorization token to get response if it is valid

const requireAuth = async (req, res, next) => {
    console.log(authorization);
    const config = {
        headers: {
          'Authorization':  authorization
        }
      }
    //verify authentication credentials with authServer
    try{
        axios.post('http:localhost:5000/api/name/user/validate', config)
        .then((response) => { 
            console.log(response);
            console.log("adssdasd");
            return next();
        })
        .catch((error) => {
            console.log(error);
        })
    } catch (error) {
        console.log(error);
        res.status(401).json({error: "Request is not authorized"});
    }
}
0 Answers
Related