Hello World...

nodejs url, querystring module 예제 본문

node.js

nodejs url, querystring module 예제

FaustK 2020. 1. 16. 23:53

예제1)

const url = require('url');
const qs = require('querystring');

// WAHTWG parse 방법
const myurl = new url.URL('https://www.naver.com:9000/user?name=mina#hash');
console.log(myurl);

// 기존 url parse 방법
const parsedUrl = url.parse(
  'https://www.naver.com:9000/user?name=mina&name=sherlock#hash'
);
console.log(parsedUrl);

const query = qs.parse(parsedUrl.query);
console.log(query);

const { name } = query;
console.log(name);

console.log(qs.stringify(query));

 

 

예제2)

const url = require('url');
const qs = require('querystring');

const parseUrl = url.parse('httt:127.0.0.1:3000/c/messages?roomname=seoul');
console.log(url.format(parseUrl));
console.log(parseUrl);

const param = qs.parse(parseUrl.query);
console.log(param);
-----console----

httt:127.0.0.1:3000/c/messages?roomname=seoul
Url {
  protocol: 'httt:',
  slashes: null,
  auth: null,
  host: '127.0.0.1:3000',
  port: '3000',
  hostname: '127.0.0.1',
  hash: null,
  search: '?roomname=seoul',
  query: 'roomname=seoul',
  pathname: '/c/messages',
  path: '/c/messages?roomname=seoul',
  href: 'httt:127.0.0.1:3000/c/messages?roomname=seoul'
}
[Object: null prototype] { roomname: 'seoul' }

 

 

 

참고)

https://dololak.tistory.com/95

Comments