Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:
Hi there. I’m trying to pass two challenge points:

You can POST a URL to /api/shorturl and get a JSON response with original_url and short_url properties. Here’s an example: { original_url : ‘https://freeCodeCamp.org’, short_url : 1}

If you pass an invalid URL that doesn’t follow the valid http://www.example.com format, the JSON response will contain { error: ‘invalid url’ }

I created a logic that are working perfectly, however failing on test.
Can anyone take a look please? Link to my replit project is below

Your project link(s)

githubLink: GitHub - will-s-205/fcc-boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project.

solution: fcc-boilerplate-project-urlshortener - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - URL Shortener Microservice

Link to the challenge:

Your URL validation is not correct. You are returning {"error":"invalid url"} for valid URLs. Which also causes one of them to use undefined as a value for one of the tests.

There are npm packages you can use for URL validation or you can use the suggested Node dns.lookup method.

Where do I get all valid URLs?

I don’t understand the question.

It is not a list of URLs, it is a heuristic for what a valid URL looks like. Just like your regex but one that actually works.

Edit: Well, I guess the dns approach relies on an actual lookup succeeding.

https://nodejs.org/api/dns.html#dnslookuphostname-options-callback

When doing the backend challenges you should get in the habit of opening the browser network tab and looking at the request/response. Look at the URL, the payload, and the response as you submit your code.

Good point. Thx for advice

I used this chunk of code from doc you provided

  if (validUrl.isUri(url)){
    console.log('Looks like an URI');
    res.json({
      original_url: url,
      short_url: 1 // REPLACE IT BY DYNAMIC VARIABLE
    });
} else {
    console.log('Not a URI');
    res.json({
      error: "invalid url"
    });
}

But still no luck.
Passing first statement for valid URL
And failing next one for invalid URL

By any chance could it be some hidden error/bug in challenge test suit?

  • Use the isWebUri method.

  • Only save the URL data to the DB if the URL is valid.

  • Make sure you save the data properly to the DB.

  • Make sure the short URL value is unique.

  • Create a GET route for the /api/shorturl/<short_url> endpoint and redirect to the full URL.

That is the last thing you should worry about. Never go there until you have exhausted all other options (which you have not at all done). It is a red herring and it is never a good idea to blame the tests when your code fails. It is much more likely that your code is wrong and the tests are telling you it is wrong than the other way around.

I’m not arguing as for the last point. Thanks for advice anyway.
Issue fixed. Working now. isWebUri helped. Thanks

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