Wednesday, March 4, 2015

Easy URL Parsing With Isomorphic JavaScript

Most web applications require URL parsing whether it's to extract the domain name, implement a REST API or find an image path. A typical URL structure: URL structure You can break a URL string into constituent parts using regular expressions but it's complicated and unnecessary…


Server-side URL Parsing


Node.js (and forks such as io.js) provide a URL API: [code language="javascript"] // Server-side JavaScript var urlapi = require('url'), url = urlapi.parse('http://ift.tt/1AKAiy4'); console.log( url.href + '\n' + // the full URL url.protocol + '\n' + // http: url.hostname + '\n' + // site.com url.port + '\n' + // 81 url.pathname + '\n' + // /path/page url.search + '\n' + // ?a=1&b=2 url.hash // #hash ); [/code]

Client-side URL Parsing


There's no equivalent API in the browser. But if there's one thing browsers do well, it's URL parsing and all links in the DOM implement a similar Location interface…

Continue reading %Easy URL Parsing With Isomorphic JavaScript%




by Craig Buckler via SitePoint

No comments:

Post a Comment