Cant pass tests despite the proyect work. APIS&Microservices URLShortener

I can’t pass the second and third tests of the urlshortener proyect, in the apis and microservices certification. Link to the challenge: https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/url-shortener-microservice

The problem is that I can’t pass these tests even if my project does exactly what they ask for:
-When you fill in the post form, it returns a json with the typed url and with its generated short version.
-If you type /api/shorturl/<short_url> with the url given above it redirects you to the page you typed at the begining.

Both the first and the last test pass perfectly
I am desperate and any help is welcome, thank you very much.

My replit proyect:
solution: https://replit.com/@julianmenav/boilerplate-project-urlshortener

Browser info:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

There’s at least two issues.

First, you’re storing the URLs in an array in the program. That only works as long as that instance of the program is the only one running and never stops. You really need to store your URL data in a database; MongoDB accessed with mongoose is the typical way with these projects. The project may pass regardless of this.

Second, not all URLs contain www, so this check is missing valid URLs:

  cleanUrl = url.replace(/^https?:\/\/www./g, "").replace(/\/$/, "")

as shown when I modify your callback (on a fork)

  dns.lookup(cleanUrl, (err, addr, fam) => {
    if (err) {
      console.log(`${req.body.url} has been marked invalid`);
      return res.json({error: "invalid url"});
    }

and get output like

https://boilerplate-project-urlshortener-9.jeremyagray.repl.co/?v=1633731844367 has been marked invalid
https://boilerplate-project-urlshortener-9.jeremyagray.repl.co/?v=1633731844919 has been marked invalid

which are valid URLs. If you are going to use dns.lookup(), you need to strip the protocol and slashes from the beginning of the URL and any route parameters from the end so that https://boilerplate-project-urlshortener-9.jeremyagray.repl.co/?v=1633731844367 becomes boilerplate-project-urlshortener-9.jeremyagray.repl.co when you check it.

1 Like

Hey there.

Thank you very much for your response Jeremy.

The main problem was as you said that to use dns.lookup I had to cut the parameters of the final part. Just by solving this I managed to pass the tests.

I think the most valuable lesson I took away is that it would have been a good idea to log the incoming requests by console so I could have found the problem and solved it. Thank you very much for this, it will help me in the future.

I also note the MongoDB issue, for this project is not necessary but it is something important in general.

Regards

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