Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Parsing a URL into its various components


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.

Getting ready

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.

How to do it...

To demonstrate this process, we create a collection of parsed URL properties, execute the parse() function, and then iterate over the results:

  1. Import Node's url module:

    url = require 'url'
  2. Execute the parse() function with a sample URL:

    address = 'http://coffeescript.org:80/?r=home/#loops'
    urlInfo = url.parse address, true
  3. 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}"

How...