How to make your own local URL Shortening Server

How to make your own local URL Shortening Server

"In this post, I am going to emulate a link shortening utility being employed in my work's intranet."


So, my corporate have this amazing tool call goto/ that allow anyone to shorten any URL into their own choice of keywords. For example, I could type goto/kitten into the URL bar at the top and have it redirect to this Cats Subreddit (https://www.reddit.com/r/cats/).

This is very useful for URL paths that are under long subdomains or with very long query/path. Something you found quite often in the corporate world, ridden by many intranet URL's and IP's to remember.

The problem though, arise when I came home to my personal computer. I became so used to the tool that I occasionally type these goto/links at home too!

To solve this problem, I set out to try recreate the same function myself!

Redirect Server using Node JS and Express

Now, I used Node JS and Express before, but I am no expert in it.

Here is a very basic script I used:

// index.js
var express = require('express');
var app = express();

app.get('/udm', function(req, res){
    res.redirect('https://www.udemy.com/home/my-courses/learning/')
})

app.listen(80, () => {
    console.log('Start listening on port 80')
})

To achieve the same effect as the ones at my work, I need the redirect to go through without me specifying my server's port (e.g. goto:3000/test doesn't look pretty). As such, the server need to listen on port 80, the default HTTP port. Browsers are sending request to IP addresses through this port by default.

After running this server, I can go to http://localhost/udm and have it correctly redirect me to Udemy page.

Now, to have goto be the redirect keyword instead of localhost, we need to tell the OS to redirect goto to localhost. To do this, we can edit the hosts file located at /etc/hosts ( c:\Windows\System32\Drivers\etc\hosts on Windows). Note that you will need to have root or admin permission to complete this step.

Add this entry to the file then save.

127.0.0.1       goto

What this does is telling the OS to redirect names on the right to addresses on the left of each entry. In this case, it tells the OS to redirect any request sent to goto to 127.0.0.1 or our localhost.

With this, our goto/udm should now work.

Conclusion

With this simple script, it allow me to:

  1. Shorten any URL with my own custom keywords.
  2. More or less verify the theory I have for how the one at my work functions. Freeing me from the itch I have everytime I use it!

Improvement

  1. Move the script to a proper server on the network.

Right now, this would only work on my laptop, and I need to have that Node JS server running on it all the time. What I will try to do next is to containerize this app then move it to run on my Raspberry Pi that is currently running Pi Hole on my local network.

2. Implement better way to add new entries.

The version at my work also have a web interface that anyone can go on to register a new shorten link. Right now, all links are hard coded to each /<endpoints>. Ideally, this should be changed to a wildcard, then have the URL be parsed by the script. There should then be a lookup table of some sort that the app can look up from.


Written by Romson Preechawit

Hi there! I'm a typical geek, designer, developer. Love coding, design, photography, reading, and a lot more. English is not my first language so please help correct my grammar! :)

Romson Preechawit