Skip to content

Latest commit

Β 

History

History
227 lines (156 loc) Β· 6.67 KB

2019-05-22-nodejs-url.md

File metadata and controls

227 lines (156 loc) Β· 6.67 KB

2019λ…„ 4μ›” 5일

Built-in module: url


인터넷 μ£Όμ†Œλ₯Ό μ‰½κ²Œ μ‘°μž‘ν•˜λ„λ‘ λ„μ™€μ£ΌλŠ” λͺ¨λ“ˆ

url 처리λ₯Ό ν•˜λŠ” 두 가지 방법

  1. WHATWG ν˜•μ‹ (node version 7μ—μ„œ μΆ”κ°€λœ ν˜•μ‹)

  2. κΈ°μ‘΄ λ…Έλ“œ url ν˜•μ‹

WHATWG_node_url

  • κ°€μš΄λ° https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash을 κΈ°μ€€μœΌλ‘œ

  • μ•„λž˜κ°€ WHATWG ν˜•μ‹, μœ„μ—κ°€ κΈ°μ‘΄ λ…Έλ“œ url ν˜•μ‹μ΄λ‹€.

1. WHATWG ν˜•μ‹

  • 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
*/

2. κΈ°μ‘΄ λ…Έλ“œ url ν˜•μ‹

  • 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 속성과 κ°™λ‹€.

μ°Έκ³ ) λ…Έλ“œ url ν˜•μ‹μ„ κΌ­ μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” 경우

  • μ£Όμ†Œκ°€ host λΆ€λΆ„ 없이 pathname λΆ€λΆ„λ§Œ μ˜€λŠ” 경우 (book/bookList.apsx)

  • μœ„μ˜ κ²½μš°μ—λŠ” WHATWG ν˜•μ‹μœΌλ‘œ μ£Όμ†Œλ₯Ό 처리 ν•  수 μ—†λ‹€.

WHATWG ν˜•μ‹μ˜ searchParams 객체

  • 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κ°€ μ—¬λŸ¬κ°œμΈ 경우 &둜 κ΅¬λΆ„ν•œλ‹€.

searchParams κ°μ²΄μ—λŠ” λ‹€μ–‘ν•œ λ©”μ„œλ“œκ°€ μžˆλ‹€

  • μ΄λŸ¬ν•œ λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄ 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 λͺ¨λ“ˆμ„ ν•œ 번 더 μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” λ²ˆκ±°λ‘œμ›€μ΄ μžˆλ‹€.

Reference

  • Node.jsκ΅κ³Όμ„œ