Url shortener microservice - using axios instead of dns

Hey all.

This is actually my first post on the forum. I’ve been coding on and off as a bit off a hobby for a while, but I’ve decided I’m going to start focusing on it more as of the last few months. I’ve never really shared any code or received much feedback other than what my console was giving me :stuck_out_tongue: but I’m hoping that with some feedback and some interaction with a community my progression will accelerate quite a bit, i’ve looked around at a few of these free sites, and some paid courses and freeCodeCamps community has definitely stuck out too me.

Anyway, Below is my solution to the url shortener microservice. The hint suggested using the dns module to see if the submitted url pointed to a valid site, but i decided to use axios because i figured it would give a bit more control over what sort of sites i allowed into the database, for example with axios, if i wanted to change the app such that the supplied url must get a status 200, i would have to change one line. with dns all that seems to matter is if the domain is valid, which is nice, but i wanted to be able to test if the path was valid too. so axios seemed like a better solution. Am i off on this? Also, is there way to basically “index” a schema similar to an array? I thought I remembered a very elegant solution to this, but I haven’t been able to find it online. I’m currently keeping a counter in the database and incrementing it every time a new url is successfully added, then using that count as the short url. I could also just find all the urls and count them before i create the new url, but that didn’t seem like a very good solution, although neither does creating a whole new schema type in the database for one counter. I’m not really sure, but here it is.

github path - github. com/M-Michelini/url-shortner

glitch code - solid-drop.glitch. me

Sorry if I explained anything poorly, I’m quite tired and just wanted to get this up real quick before I went to bed, just let me know and I’ll try and explain it better tomorrow, also sorry about the crappy link, it wouldn’t let my post have links in it because it’s my first post. Cheers.

1 Like

No sounds fine to me i’d probably do the same because ultimately your checking the url and not just the domain.

Make sure you do the check within the nodejs app for security. :smile:

Thanks :slight_smile:

Am I not currently doing the url check within the nodejs app? I may not fully understand what you mean, where else would i run the check? potentially in the database i guess, but why would the be unsecure?

I just thought you may use axios clientside and send the data up to the server. axios is a good one in that sense

You got it right though!!

Why because if the check is made client side, an attacker could spoof this and send up invalid data which you’d save.

ahhh okay, I see what you are saying. Thanks

I like the idea of axios doing all the heavy lifting for url verification, in fact I just changed my verification middleware to use axios’ positive response and attach a verified url to the request, much simpler, using dns and the native node url parser was complicating my middleware and I like simple. Although I do wonder whether a little efficiency is sacrificed as axios has to literally go out and fetch the site before the response, where as dns just looks up tables ?

With respect to the shortened url, I don’t think the user stories stipulate that they have to be ordered numbers, for instance I used a randomized 5 character capitalized alphanumeric key. But in any event, couldn’t you just take the length of the returned documents array and simply add one for a successful new url entry? granted I have not looked at your model in detail so I may be missing something.

Hey Dereje1, thanks for the feedback :slight_smile: My other option was going to be to find all the urls in the collection, count them and set that as the short url, but i figured it was more efficient to find 1 counter, then find 1 url, instead of finding every single url document before every post request. I’m not really sure it would change much, and the solution you mentioned is definitely neater, but if there were something like 1,000,000 urls, it seems like the way i chose would perform better, although im not actually sure if that’s the case, i may have to do some testing.