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.
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:
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.
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.