728x90
url 모듈 정의
url 모듈을 통해서 URL 문자열을 객체로 바꾸거나 URL 객체를 문자열로 변환 가능하며,
파싱과 조합을 사용하여 URL과 관련된 정보를 가져올 수 있습니다.
url 모듈 함수
- parse(urlStr, [, parseQueryString=false, slashesDenoteHost=false])) : URL 문자열을 URL 객체로 변환하여 리턴합니다.
- format(urlObj) : URL 객체를 URL 문자열로 변환하여 리턴합니다.
- resolve(from, to) : 매개변수를 조합하여 URL 문자열을 생성하여 리턴합니다.
예제
'http://user:pass@host.com:8080/p/a/t/h?query=string#hash' 주소를 객체로 쪼게어 본다면 다음과 같다고 한다.
┌─────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬┬───────────┬─────────────────┬───────────────────────────┬───────┤
│ protocol ││ auth │ host │ path │ hash │
│ ││ ├──────────┬──────┼──────────┬────────────────┤ │
│ ││ │ hostname │ port │ pathname │ search │ │
│ ││ │ │ │ ├─┬──────────────┤ │
│ ││ │ │ │ │ │ query │ │
" http: // user:pass @ host.com : 8080 /p/a/t/h ? query=string #hash "
│ ││ │ │ │ │ │ │ │
└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘
(all spaces in the "" line should be ignored -- they are purely for formatting)
이렇게 파싱된 url 객체를 잘 활용할 수 있을 뿐더러, resolve, format 함수를 통해 조합하고 다시 url 객체로 변경할 수도 있다.
728x90
'JavaScript > Node.js' 카테고리의 다른 글
Logging 모듈 Winston (0) | 2023.05.03 |
---|---|
Node.js에서 Python 함수 호출하기 - spawn() (0) | 2023.05.03 |
자식 프로세스(child_process) (0) | 2023.02.07 |
Node.js 기초와 모듈(내장 모듈, npm, 사용자정의 모듈) (0) | 2023.02.04 |
사용자 정의 모듈 (0) | 2023.02.04 |