An own URL shrinker, why?
Which benefits do you get with it? A complete guide to write/fork it yourself.
I always asked myself 'Why would I ever need an own URL shrinker?' and I think you did so.
Why do you need a URL shrinker?
First things first, the most obvious reason is that you are able to shrink URLs, and you have no limitations. If you used a commercial URL shrinker like bitly or something else you have such limits. Another key factor is that you can use your own URL and that's awesome. It also looks cool.
How to write your own URL shrinker?
Basics
I used node.js as runtime. This simply because it can handle backend and frontend for me - this mean that I have not that much work to pass data to my backend. For frontend, I used express wit ejs. Simply because I have the most experience with that booth. For data storage I use MongoDB and for settings a .env file. So all dependencies are mention below. The package shortId is used for the generated URLs.
server handling
First you have to implement and require the packages you use. They are shown in the following picture.
After that you should connect to your database. In my case it's MongoDB.
After that I set some basic express settings for example view engine and stuff like that.
After that I specified some routes. First the '/' route which shows all shorted URLs and allows shrinking new ones. Then the post '/shortUrls' which generates new shortUrls.
The last one is '/:shortUrl' which handles every incoming request and count up for URL statistic.
The last thing to do in that file was the 'app.listen' command which starts the server on a specific port.
Index ejs view
First things in an ejs file which works for me like a HTML file is the head section.
After that the body is starting with a form to shrink new URLs.
The last selection in that file is, the table for already generated URLs.
shrunk model
Then I created a model for the database. It looks like that
There is also they are where I generate the shrunk URL.
Don't forget
I also have a public folder for the favicon. It looks like that:
I have this project public to use at wlinc.de and open-source at GitHub. I'm looking forward to your feedback and pull-requests/issues.