Axios 에서 로컬 호출시 (127.0.0.1/localhost) 에러 처리

2023. 4. 26. 16:16· 자바스크립트
const https = require("https");

여기 아주 간단한 코드가 있다.

 Node.js 에서 Axios를 통해 특정 Site URL에 request를 송신 후, 응답을 받아오는 코드이다.

# hosts 를 통해 127.0.0.1 을 local.dev.kr 로 매핑해둔 상태

const axios = require("axios");

const axiosOption = {
        timeout: 4000,
        headers: {
          "User-Agent": "Chrome",
        },
      };

      
const instance = axios.create(axiosOption);

instance.get("http://local.dev.kr").then((res) => { })

별거 아닌 코드인데도 불구하고 바로 에러를 뱉어주신다. 

에러코드

AxiosError: write EPROTO 4864335056:error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER:../../third_party/boringssl/src/ssl/tls_record.cc:231:

    at AxiosError.from (/Users/shinbc89/doctorworks/dwaidata-client/node_modules/axios/dist/node/axios.cjs:829:14)
    at RedirectableRequest.handleRequestError (/Users/shinbc89/doctorworks/dwaidata-client/node_modules/axios/dist/node/axios.cjs:2975:25)
    at RedirectableRequest.emit (node:events:525:35)
    at eventHandlers.<computed> (/Users/shinbc89/doctorworks/dwaidata-client/node_modules/follow-redirects/index.js:14:24)
    at ClientRequest.emit (node:events:513:28)
    at TLSSocket.socketErrorListener (node:_http_client:502:9)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  syscall: 'write',
  code: 'EPROTO',
  errno: -100,
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },

이 오류는 HTTPS 요청 시 SSL 버전이 맞지 않아 발생하는 것으로 보이는데
이를 해결하기 위해 다음과 같은 방법들을 시도해 볼 수 있겠지만, 내가 해결했던 방안은 아래와 같다. 

반응형

해결방안

Axios에서 SSL 쪽 rejectUnauthorized 속성을 false로 설정.
해당 부분은 http 그것도, 로컬에서 테스트할때만 사용되기 때문에 protocol에 따라 분기처리를 하였다.

const axios = require("axios");
const https = require("https"); // ---------- 추가

const axiosOption = {
    timeout: 4000,
    headers: {
        "User-Agent": "Chrome",
        Accept: "text/html",
    },
};

const url = "http://local.dev.kr";
const protocol = url.includes("https://") ? "https" : "http"

// -------------- 코드 추가 START
if (protocol === "http") {
	axiosOption.httpsAgent = new https.Agent({ rejectUnauthorized: false });
}
// -------------- 코드 추가 END
      
const instance = axios.create(axiosOption);


instance.get("http://local.dev.kr").then((res) => { })

 

 

반응형
저작자표시 비영리 변경금지 (새창열림)
신·기록
신·기록
개발 관련 이슈 해결방안, 활용 방법, 제품 리뷰 등을 기록하는 개인 블로그
신·기록
문제해결집
신·기록
전체
오늘
어제
  • 분류 전체보기 (175)
    • 뉴스 (9)
    • 제품 리뷰 (19)
    • 운영체제 및 서버 (53)
      • Linux (27)
      • Mac OS (8)
      • Proxmox (6)
      • Nginx (2)
      • Raspberry Pi (3)
      • Odroid M2 (4)
    • AI 인공지능 (7)
    • 모바일 (4)
      • Flutter (1)
      • 안드로이드 (1)
    • 데이터베이스 (11)
      • Mysql (9)
    • 자바 (11)
      • Spring Framework (9)
    • 자바스크립트 (19)
      • Node JS (4)
      • Electron Framework (10)
      • React & Next.js (4)
    • 클라우드 플랫폼 (4)
    • 학습 (4)
    • 이슈 (21)
    • 비디오게임 에뮬레이터 (2)
    • CPU 성능 순위 (1)
    • 그래픽카드 성능 순위 (1)
    • 기타 (7)

블로그 메뉴

  • 이용 원칙
  • 태그
  • 방명록

인기 글

최근 글

05-22 17:36
관련 문의는 방명록에 부탁드립니다.
본 블로그의 스킨 출처는 아래와 같습니다.
hELLO · Designed By 정상우.v4.2.2
신·기록
Axios 에서 로컬 호출시 (127.0.0.1/localhost) 에러 처리
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.