This is the interesting part where the actual URL encoding and decoding happens.
Before we jump into the code development, let's explore the basic theory behind URL shortening. Our requirement is as follows:
Given a long URL such as http://stackoverflow.com/questions/tagged/node.js, generate a unique URL that is shorter, such as localhost:3000/3Ys
.
First, let's look at actually generating a unique key from our original URL, in this case 3 years, which is at the end of our localhost URL.
To satisfy the uniqueness requirement, we can use a special class of functions called bijective (https://en.wikipedia.org/wiki/Bijection) functions, which guarantee a 1-to-1 mapping. For explanation purposes, a bijective function basically says:
"A decoded URL is mapped to exactly one key, and a key is mapped to exactly one decoded URL"
We are going to use a base58 algorithm to fulfil our URL shortening and decoding task.
We are going to convert a unique integer ID (which is in base10) to...