Our modern applications often need to communicate with a backend server. In the server-side code or service code, we may need to analyze the requesting URLs.
In this section, we will investigate ways to parse URLs into their constituent components.
We will be using Node's built-in URL parsing functionality to perform our URL manipulations. There is nothing additional to install to perform this task.
To demonstrate this process, we create a collection of parsed URL properties, execute the parse()
function, and then iterate over the results:
Import Node's
url
module:url = require 'url'
Execute the
parse()
function with a sample URL:address = 'http://coffeescript.org:80/?r=home/#loops' urlInfo = url.parse address, true
Display each of the
urlInfo
object's properties:for property of urlInfo if urlInfo[property]? value = JSON.stringify urlInfo[property] if value? console.log "#{property.toUpperCase()}: #{value}"