URL Shortener Microservice Submission Failing

Tell us what’s happening:
I am not able to pass the 2nd and 3rd testcase although my project is working fine when running locally.

Your project link(s)

githubLink: GitHub - d02ev/freecodecamp--url-shortener-microservice: my solution to the backend certification project (URL Shortener MIcroservice)
solution: https://fcc--url-shortener-microservic.herokuapp.com

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0

Challenge: URL Shortener Microservice

Link to the challenge:


Your main problem is the URL validation. Since you’re only removing the https:// part of the URL, the DNS.lookup will also fail and return an error.

What you could do is this to validate the URL:

let parsedUrl;

try {
    parsedUrl = new URL(req.body.url);
} catch (e) {
    // If there's an error, then the URL is not valid
    return req.json({error: 'invalid url'});

// Now you got a valid URL with more utility methods
// like the one used to retrieve only the hostname:
DNS.lookup(parsedUrl.hostname, ....);

Read the Node URL object documentation here.

I hope it helps :slight_smile:.

1 Like

Thanks @skaparate this has solved my problem :slight_smile:

@skaparate seems there’s another problem, I went by your solution but this time I am failing the last case, the case which checks for an invalid url all the other cases have been passed but the last one.

Hmmm, yeah, I see… Validate the URL with a regex before the try...catch. Maybe this one may help.

Do not remove the try...catch since you can still benefit with the URL object.

Thank you very much again for the help @skaparate, I have successfully completed the project.

1 Like

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