Axios 서버에서 axios요청을 다른서버로 보낼시 에러(https)
NestJS(NodeJS)서버에서 다른 NestJS(NodeJS)서버로 GET요청을 할때 발생된 에러 입니다.
아래는 터미널 출력 내용입니다.
Axios URL : https://goodgame:5000/users/single
fetch error : AxiosError: unable to verify the first certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1530:34)
at TLSSocket.emit (node:events:394:28)
at TLSSocket._finishInit (node:_tls_wrap:944:8)
at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:725:12) {
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
위보다 훨씬 길었지만 중요한 것은 fetch error와 code입니다.
위 문제를 해결하기 위해서 axios에 코드를 추가해 줘야 합니다.
const https = require('https');
const response = await axios
.get(process.env.NESTJS_RESTAPI_URL + '/users/single', {
headers: {
'Content-Type': 'application/json',
authorization: 'Bearer ' + token,
},
httpsAgent: new https.Agent({
rejectUnauthorized: false, //허가되지 않은 인증을 reject하지 않겠다!
}),
})
.then((data) => {
console.log('result data : ', data);
return data.data.data;
});
코드1) httpsAgent추가
https통신을 할때 인증을 받지 않은 클라이언트(여기서는 NestJS서버)가 요청을 하면 에러를 띄워주는데 이때 httpsAgent의 rejectUnauthorized 값을 false로 하면 무시하게 됩니다.
댓글
댓글 쓰기