Quantcast
Channel: Node.js – SitePoint
Viewing all articles
Browse latest Browse all 225

Easy URL Parsing With Isomorphic JavaScript

$
0
0

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://site.com:81/path/page?a=1&b=2#hash'); 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%


Viewing all articles
Browse latest Browse all 225

Latest Images

Trending Articles



Latest Images