블록체인/Ethers & web3
블록체인 RPC_URL 엔드포인트에서 마지막 "/" 제거하기
체인의정석
2022. 5. 4. 15:05
728x90
RPC_URL을 환경변수로 받아오는데 계속해서 마지막에 "/"가 자동 추가되어서
"TypeError: Only absolute URLs are supported" 해당에러가 계속 발생하였다.
이를 해결하기 위해서 서치한 결과
https://thewebdev.info/2022/01/20/how-to-remove-last-segment-from-url-with-javascript/
How to remove last segment from URL with JavaScript? - The Web Dev
Spread the love Related Posts How to Remove the Query String from URL with JavaScript?Sometimes, we want to remove the query string part of a URL with JavaScript. In… How Validate YouTube URL with JavaScript Regexes?Sometimes, we want to validate YouTube
thewebdev.info
const url = 'http://example.com/foo/bar'
const newUrl = url.slice(0, url.lastIndexOf('/'));
console.log(newUrl)
이렇게 하면 된다고 한다.
자바스크립트의 slice 함수와 url.lastIndexOf를 섞어서 사용하는 것으로 마지막 / url이 들어간 부분을 지워버릴 수 있다.
이걸 조금 커스터마이징하여
let rpc_url = process.env.RPC_URL!
console.log("url before",rpc_url)
let lastUrl = rpc_url.substring(rpc_url.length-1,rpc_url.length)
if(lastUrl == "/") {
rpc_url = rpc_url.substring(rpc_url.length-2,rpc_url.length-1)
}
이렇게 바꿔 보았다.
근데 envrc 에서 = 등호뒤의 띄어쓰기를 없애니 해당 오류가 사라져서 이건 기록만 남기고 사용하지 않기로 하였다.
728x90
반응형