URL Shortener API Feedback

Good evening all. I finally finished the URL Shortener API. I started it a while ago and got stuck on a few things, so I continued with the lessons and came back to it this week.

It seems like a simple thing but I had some trouble figuring out the logic. Even now I’m not sure if I’ve made it overly-complicated.

https://kindly-fisherman.glitch.me/

What it does:
-User posts input to /api/shorturl/new
-Checks that input isn’t blank.
-Strips off http(s) and uses dns.lookup to check if input is a valid website.
-Checks the database if input is already there.
-If input isn’t in database:
–Counts records in database.
–Adds record to database.
–Returns input and id in the database.
-If input is in database:
–Returns input and id in the database.

And then:
-User gets input to /api/shorturl/:input
-Checks that input is just numeric characters.
-Checks the database for input.
-If input isn’t in database:
–Returns error message.
-If input is in database:
–Redirects to website.

Areas to improve:
-The dns.lookup was flagging some websites that work okay.
-I counted the records to figure out the new record’s id, but there’s probably a better way to do this.
-Since the tail end of the /api/shorturl/new section returns similarly, I’d like to rewrite the code so it doesn’t repeat.

1 Like