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.
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.
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);
}
})
})
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.
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.