2019λ 4μ 5μΌ
μΈν°λ· μ£Όμλ₯Ό μ½κ² μ‘°μνλλ‘ λμμ£Όλ λͺ¨λ
-
WHATWG νμ
(node version 7μμ μΆκ°λ νμ) -
κΈ°μ‘΄ λ Έλ url νμ
-
κ°μ΄λ°
https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash
μ κΈ°μ€μΌλ‘ -
μλκ°
WHATWG νμ
, μμκ°κΈ°μ‘΄ λ Έλ url νμ
μ΄λ€.
-
url λͺ¨λ μμ μλ
URL μμ±μ
λ₯Ό μ¬μ©νλ€. -
URL μμ±μ
μ μ£Όμλ₯Ό λ£μ΄ κ°μ²΄λ₯Ό λ§λ€λ©΄ μ£Όμκ° λΆλΆλ³λ‘ μ 리λλ€. -
WHATWG νμμλ§ μλ μμ±:
username, password, origin, searchParams
const url = require('url');
const { URL } = url;
const myURL = new URL('http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor');
console.log('new URL():', myURL);
console.log('url.format():', url.format(myURL));
- μμ κ²°κ³Όλ λ€μκ³Ό κ°λ€.
/*
new URL(): URL {
href:
'http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor',
origin: 'http://www.gilbut.co.kr',
protocol: 'http:',
username: '',
passwor`d`: '',
host: 'www.gilbut.co.kr',
hostname: 'www.gilbut.co.kr',
port: '',
pathname: '/book/bookList.aspx',
search: '?sercate1=001001000',
searchParams: URLSearchParams { 'sercate1' => '001001000' },
hash: '#anchor' }
url.format(): http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor
*/
url.parse
μ μ£Όμλ₯Ό λ£μΌλ©΄ μ£Όμκ° λΆλΆλ³λ‘ μ 리λλ€.
const url = require('url');
const parsedUrl = url.parse('http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor');
console.log('url.parse():',parsedUrl);
console.log('url.format():', url.format(parsedUrl));
- μμ κ²°κ³Όλ λ€μκ³Ό κ°λ€.
/*
url.parse(): Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.gilbut.co.kr',
port: null,
hostname: 'www.gilbut.co.kr',
hash: '#anchor',
search: '?sercate1=001001000',
query: 'sercate1=001001000',
pathname: '/book/bookList.aspx',
path: '/book/bookList.aspx?sercate1=001001000',
href:
'http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor' }
url.format(): http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor
*/
-
WHATWG νμκ³Ό κΈ°μ‘΄ λ Έλ url νμ λΉκ΅
-
WHATWG νμμ
username
κ³Όpassword
μ λ Έλ url νμμauth
μμ±κ³Ό κ°λ€. -
WHATWG νμμ
searchParams
λ λ Έλ url νμμquery
μμ±κ³Ό κ°λ€.
-
-
μ£Όμκ° host λΆλΆ μμ΄
pathname
λΆλΆλ§ μ€λ κ²½μ° (book/bookList.apsx
) -
μμ κ²½μ°μλ WHATWG νμμΌλ‘ μ£Όμλ₯Ό μ²λ¦¬ ν μ μλ€.
-
http://www.gilbut.co.kr/?page=3&limit=10&category=nodejs&category=javascript
-
μμ μ£Όμμμ
?page=3&limit=10&category=nodejs&category=javascript
λΆλΆμ΄search
μ΄λ€. -
WHATWG νμμ
search
λΆλΆμsearchParams
λΌλ νΉμν κ°μ²΄λ₯Ό λ°ννλ―λ‘ μ μ©νλ€. -
search
λΆλΆμ λ³΄ν΅ μ£Όμλ₯Ό ν΅ν΄ λ°μ΄ν°λ₯Ό μ λ¬ ν λ μ¬μ©νλ€. -
search
λ λ¬Όμν(?
)λ‘ μμνλ©°key = value
νμμΌλ‘ λ°μ΄ν°λ₯Ό μ λ¬νλ€. -
key
κ° μ¬λ¬κ°μΈ κ²½μ°&
λ‘ κ΅¬λΆνλ€.
- μ΄λ¬ν λ©μλλ₯Ό μ΄μ©ν΄
search
λΆλΆμ μ‘°μνλ€.
const { URL } = require('url');
const myURL = new URL('http://www.gilbut.co.kr/?page=3&limit=10&category=nodejs&category=javascript');
console.log('searchParams:', myURL.searchParams);
/*
searchParams: URLSearchParams {
'page' => '3',
'limit' => '10',
'category' => 'nodejs',
'category' => 'javascript' }
*/
-
getAll(key)
:key
μ ν΄λΉνλ λͺ¨λ κ°λ€μ κ°μ Έμ¨λ€.- μ μμ μμ
category
λΌλkey
κ°μλ λ κ°μvalue
(nodejs, javascript
)κ° λ€μ΄μλ€.
- μ μμ μμ
-
get(key)
:key
μ ν΄λΉνλ 첫 λ²μ§Έ κ°λ§ κ°μ Έμ¨λ€. -
has(key)
: ν΄λΉkey
κ° μλμ§ κ²μ¬νλ€.
console.log(`searchParams.getAll('category'):`, myURL.searchParams.getAll('category'));
console.log(`searchParams.get('limit'):`, myURL.searchParams.get('limit'));
console.log(`searchParams.has('page'):`, myURL.searchParams.has('page'));
/*
searchParams.getAll('category'): [ 'nodejs', 'javascript' ]
searchParams.get('limit'): 10
searchParams.has('page'): true
*/
-
keys()
:searchParams
μ λͺ¨λkey
λ₯Όiterator
κ°μ²΄λ‘ κ°μ Έμ¨λ€. -
values()
:searchParams
μ λͺ¨λvalue
λ₯Όiterator
κ°μ²΄λ‘ κ°μ Έμ¨λ€.
console.log(`searchParams.keys():`, myURL.searchParams.keys());
console.log(`searchParams.values():`, myURL.searchParams.values());
/*
searchParams.keys(): URLSearchParams Iterator { 'page', 'limit', 'category', 'category' }
searchParams.values(): URLSearchParams Iterator { '3', '10', 'nodejs', 'javascript' }
*/
append(key, value)
: ν΄λΉkey
λ₯Ό μΆκ°νλ€. κ°μkey
κ° μμΌλ©΄ μ μ§νκ³ νλ λ μΆκ°νλ€.
myURL.searchParams.append('filter', 'code');
myURL.searchParams.append('filter', 'squad');
console.log(`searchParams.getAll():`, myURL.searchParams.getAll('filter'));
// ssearchParams.getAll(): [ 'code', 'squad' ]
set(key, value)
: append μ λΉμ·νλ€. λ€λ₯Έμ μ κ°μkey
κ°μ΄ μμΌλ©΄ λͺ¨λ μ§μ°κ³ μλ‘ μΆκ°νλ€.
myURL.searchParams.set('filter', 'code squad');
console.log(`searchParams.getAll():`, myURL.searchParams.getAll('filter'));
// searchParams.getAll(): [ 'code squad' ]
delete(key)
: ν΄λΉkey
λ₯Ό μμ νλ€.
myURL.searchParams.delete('filter');
console.log(`searchParams.getAll():`, myURL.searchParams.getAll('filter'));
// searchParams.getAll(): []
toString()
:searchParams
κ°μ²΄λ₯Ό λ€μ λ¬Έμμ΄λ‘ ν©μΉλ€. μ΄ λ¬Έμμ΄μsearch
μ λμ νλ©΄ μ£Όμ κ°μ²΄μ λ°μλλ€.
console.log(`searchParams.toString():`, myURL.searchParams.toString());
// searchParams.toString(): page=3&limit=10&category=nodejs&category=javascript
myURL.search = myURL.searchParams.toString();
- WHATWG νμμ
searchParams
κ° κΈ°μ‘΄ λ Έλ url νμμquery
λ³΄λ€ μ μ©ν μ΄μquery
λquerystring
λͺ¨λμ ν λ² λ μ¬μ©ν΄μΌ νλ λ²κ±°λ‘μμ΄ μλ€.