1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//url 모듈 불러오기
 
var url = require('url');
 
 
 
//이런 주소를 url 모듈을 사용하면 분해할 수 있음
 
var curUrl = url.parse('https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=NodejsTest');
 
 
 
//protocal : https,  host : www.naver.com, query : ? 이후의 것 
 
 
 
//이때 curUrl 은 객체로 아래 결과와 같은 멤버들을 가지고 있다
console.dir(curUrl);
 
 

 
 
//이 객체를 다시 문자열로 합칠려면 
 
var oriUrl =  url.foramt(curUrl); 을 사용하면 된다
 
 

cs





결과 

Url {

  protocol: 'https:',

  slashes: true,

  auth: null,

  host: 'search.naver.com',

  port: null,

  hostname: 'search.naver.com',

  hash: null,

  search: '?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=NodejsTest',

  query: 'where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=NodejsTest',

  pathname: '/search.naver',

  path: '/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=NodejsTest',

  href: 'https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=NodejsTest' }




이때 

require('querystring'); 모듈을 로딩하고

querysting.parse(curUrl.query); 를 하게 도면 NodejsTest 만 얻을 수 도 있다






반응형

+ Recent posts