Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:
Why is freecodecamp bot testing a shorturl which is undefined?
I have manually tested all the cases and run fine until freecodecamp unit test it, it simply uses shorturl value as undefined and expects the output to be valid?

app.get("/api/shorturl/:su", function(req, res) {
  console.log("HI, Shorturl/number here", req.originalUrl);

  if(req.params.su == 'undefined') {
    res.json({error: "No short URL found for the given input"});
  }
  else {
    ShortUrl.findOne({short_url: req.params.su}, function(e, d) {
      if(e) {
        console.log(e);
        res.json({error: "Something wrong with database, cannot fetch the original url"});
      }
      else {
        if (d.length == 0) {
          res.json({
            "error": "No short URL found for the given input"
          });
        }
        else {
          res.redirect(d.original_url);
        }
      }
    });
  }
});

Output:

HI, Shorturl/number here /api/shorturl/undefined

Your project link(s)
live may not work

solution: boilerplate-project-urlshortener - Replit

Your browser information:

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

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

Your URL validation isn’t working.

Request URL: https://boilerplate-project-urlshortener.pbji.repl.co/api/shorturl
Payload: https://boilerplate-project-urlshortener.pbji.repl.co/?v=1695755770570
Response: `{"error":"invalid url"}`

You can use a package like valid-url (with the isWebUri function) to simplify it.

Oh so there does exist libraries that eases validations… Nice to know that. THank you so so much.

For anyone wondering, I have successfully passed this assignment, the problem was taking place with my extracting the hostname from the url using rexexp, also the reason why the url validator was not able to validate it at the first place, make sure if you are using regexp to cover all sort of cases, in case you need help creating a regexp, I recommend using chatGPT for those are beginning to using regexp in their programs. It is capable of generating complex regexp, but make sure also to prompt it using few-shot techniques instead of cutting corners. This landed me into trouble for hours, because the regexp was not including a speacial case of an url in my case which is what freecodecamp bot was using as a test.

EDIT: For anyone wondering, I have successfully passed this assignment.

The problem was taking place with my extracting the hostname from the url using rexexp, also the reason why the url validator was not able to validate it at the first place

Make sure if you are using regexp to cover all sort of cases, in case you need help creating a regexp, I recommend using chatGPT for those are beginning to using regexp in their programs. It is capable of generating complex regexp, but make sure also to prompt it using few-shot techniques instead of cutting corners.

This landed me into trouble for hours, because the regexp that ChatGPT generated did not include a speacial case of an url in my case which is what freecodecamp bot was using as a test.

(To elaborate, the regexp didn’t included hypens as part of the hostname, hence match() returned null, when codecamp used my replit url as a test “https://boilerplate-project-urlshortener.pbji.repl.co/api/shorturl”, I quickly this fixed the mistake by prompting chatGPT to troubleshoot this regexp and it gave an accurate solution for it this time)

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