입력:23/06/05수정:24/01/05
nodejs url
const url = require('url');
// 예시 URL
const urlString = 'https://www.example.com:8080/path/to/resource?key1=value1&key2=value2#fragment';
const parsedUrl = url.parse(urlString, true);
// 각 컴포넌트
const protocol = parsedUrl.protocol; // 'https:'
const hostname = parsedUrl.hostname; // 'www.example.com'
const port = parsedUrl.port; // '8080'
const path = parsedUrl.pathname; // '/path/to/resource'
const query = parsedUrl.query; // { key1: 'value1', key2: 'value2' }
const fragment = parsedUrl.hash; // '#fragment'
console.log('Protocol:', protocol);
console.log('Hostname:', hostname);
console.log('Port:', port);
console.log('Path:', path);
console.log('Query:', query);
console.log('Fragment:', fragment);
queryString
const querystring = require('querystring');
const url = 'https://www.example.com/search?query=node.js&page=1&sort=desc';
// url에서 추출하기
const parsedQuery = querystring.parse(url.split('?')[1]);
console.log(parsedQuery);
// Output: { query: 'node.js', page: '1', sort: 'desc' }
// object to queryString
const obj = { name: 'John', age: 30, city: 'New York' };
const queryString = querystring.stringify(obj);
console.log(queryString);
// Output: name=John&age=30&city=New%20York