Issues with URL Shortener

Tell us what’s happening:
I seemingly random fail and pass tests when submitting my working project link. However it works fine when testing from replit. I am alternately failing tests 2, 3 and 4. I sometimes pass 2 tests, sometimes 3, but never all 4. I have not changed my code but I keep running into this issues when submitting it.

Your project link(s)

solution: https://replit.com/@williammallady/boilerplate-project-urlshortener-5

Your browser information:

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

Challenge: URL Shortener Microservice

Link to the challenge:

Console.log the inputs (req.body, req.params, req.query) and the outputs in the routes that gave error, thus when the test spec is running, they all will be printed on your console.

Knowing the Inputs and Outputs is always crucial in coding, so that we know whether we violated some prescribed Constraints or not.

Thank you for the advice, but this does not seem to be the issue as I verified everything with console.log() while writing the code.

I did find this in my logs when running the test though:

Access to fetch at ‘https://boilerplate-project-urlshortener-5.williammallady.repl.co/api/shorturl/undefined’ from origin ‘https://www.freecodecamp.org’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

Using console.log() in development is good, but not a substitute for logging everything on a route during debugging. Consider your get route, as modified:

app.get('/api/shorturl/:urlId', bodyParser.urlencoded({ extended: false }), (req, res) => {
  console.log('hello');
  console.log(`req.body: ${JSON.stringify(req.body)}`);
  console.log(`req.params: ${JSON.stringify(req.params)}`);
  console.log(`req.query: ${JSON.stringify(req.query)}`);

  let id = req.params.urlId;

  urlObj.findOne({ short_url: id }, (err, data) => {
    if (err) {
      console.log('oops');
    } else if (! data) {
      console.log('no data');
    }
    if (!err && data != null) {
      console.log("ORIGINAL URL IS: " + data.original_url)
      return res.redirect(data.original_url);
    }
  })
})

When I run the tests against it now, it logs

hello
req.body: {}
req.params: {"urlId":"undefined"}
req.query: {}
oops

which shows that it’s not getting the URL id from the tests, which means that the tests are not getting it from your post route. Do the same logging in your post route, and you’ll see that some URLs that should be valid are marked invalid, which backs the problem all the way to your URL regular expression, which is the problem. If you look at the URLs being tested, you’ll see that they do not all start with www or http.

The CORS problems can be fixed by using the examples from the CORS exercises.

1 Like

My regex was indeed the problem. I don’t like the idea of validating the URL with a regex, but it seemed the tests were not happy with the recommended dns lookup method. I really, really appreciate your answer Jeremy.

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