Url shortener microservice working as expected but failing 2nd and 3rd tests

I’ve been working on this project for so long now and I can’t find why is it failing those 2 tests… the problem is I did exactly what’s written in the tests myself and saw it working perfectly but it still fails…

Here’s my code on repl.it: https://replit.com/@MidoZZX/boilerplate-project-urlshortener

This is my first post because I’m new here so I don’t know what else do I need to share…

1 Like

This is not correct

  var filteredUrl = url.replace(/https?:\/\//, "");

  dns.lookup(filteredUrl, function(err) {

url passed actually contains different protocol like ftp and query parameters. You use only the hostname for dns lookup. You want to create a new URL object and test the object’s protocol (http: and https are valid) is okay and then use lookup dns.lookup(new_url.hostname, …).

Later you’re actually creating an URL object and store it as the origina_url. It might work okay, but for your information, I stored req.body.url and I passed the FCC tests storing req.body.url.

1 Like

why do you say it is not correct? Maybe it’s not the perfect solution but it works that way… the way I actually did it like this was because the dns.lookup method failed obvious working urls like https://freecodecamp.org and when I looked it up ppl said it is because u need to pass it without the http/https part so that’s why I used the regex there… and it worked after that… and I only use it (filtered_url) for the dns check and after that continue using the original url for the rest of the application… Do you know what’s the problem with the actual tests though? cuz if you test my app it should be working perfectly as mentioned!

1 Like

The part for stripping http or https may be fine if that’s all you have to check. I wanted to say that it is easier to let URL object to do the work instead of doing work yourself. By having an URL object, you can easily check the protocol but reading url.protocol property.

The problem is after that. Just stripping the protocol is not enough for checking the validity of url. What happens when the passed url (this is the actual test value FCC used on my code) is like


Your code will pass


to dns lookup and come back invalid. But this url is valid, as you should be passing the hostname only


to dns lookup, not full url minus the protocol.
This is the reason why I said your code as written is not correct. It is not correct, according to the FCC tests, because your code fails the tests. Some times it is very perplexing why your seemingly perfect code is failing, but there are reasons.
I experienced the exact same issue and I fixed the problem as I mentioned in my previous post after getting help here


Oh, man… I don’t know how to thank you for this… I made this forum account solely for this problem and I actually solved it now by using the URL object (which was totally new to me) instead of the regex… Again thank you very much

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.