Linkr

Stack: Node, MongoDB

Linkr is a URL shortening service that generates a unique short link for a given resource and subsequently redirects HTTP requests directed at that link to the original URL.

New links are generated by POST requests made to the api/url/shorten endpoint. The longUrl field of the request’s body JSON specifies the URL to be shortened.

// create new short link for the google homepage

curl --request POST 'http://localhost/api/url/shorten' --header 'Content-Type: application/json' --data-raw '{"longUrl": "https://google.com"}'

If the database already contains an entry for the provided URL, that entry is returned. If the URL isn’t found, it is first checked for validity and then assigned a unique ID. A new database entry is then created and the data therein is returned in the HTTP response. This data includes the original long URL, the new short URL, and the unique ID (which comprises the path of the short URL).

// example server response from short link service

{
    "_id": "61af8a8b9d9c2b0e9c4e3924",
    "longUrl": "https://google.com",
    "shortUrl": "http://localhost/JI7EBrPYW",
    "urlCode": "JI7EBrPYW",
    "date": "Tue Dec 07 2021 11:23:39 GMT-0500 (Eastern Standard Time)",
    "__v": 0
}

The service now makes the original URL available at a path matching the previously generated ID (e.g. http://localhost/JI7EBrPYW ) via a 302 redirect.

Repo: https://github.com/alamansky/linkr