Tell us what’s happening:
I have completed the project (from my side) and ran it to check it. It works completely fine for all my tests, yet when I submit it, it fails 2 out of the 4 tests and I have no idea why. I’m stuck on this for a while now and would really like some help
Your code so far These are the 2 endpoints we have to work upon:
app.post('/api/shorturl/new', async (req, res) => {
var count;
try {
const URLToShorten = await new URL(req.body.url);
console.log(URLToShorten);
if(URLToShorten.protocol != "http:" && URLToShorten.protocol != "https:") {
return res.json({
"error": "Invalid URL"
});
}
const foundURL = await ShortURL.findOne({
original_url: URLToShorten.origin
});
if(foundURL)
return res.json({
original_url: foundURL.original_url,
short_url: foundURL.short_url
});
else {
await dns.lookup(URLToShorten.host, (err, data) => {
if(err) console.log("error is: " + err);
else console.log("data is: " + data);
});
await ShortURL.countDocuments({}, (err, data) => {
if(err) console.log(err);
else count = data;
});
const newURL = new ShortURL({
original_url: URLToShorten.origin,
short_url: count+1
});
newURL.save((err, savedURL) => {
if(err) console.log(err);
return res.json({
original_url: savedURL.original_url,
short_url: savedURL.short_url
});
});
}
} catch(err) {
res.json({
"error": "Invalid URL"
});
}
});
app.get('/api/shorturl/:shorturlno', (req, res) => {
ShortURL.findOne({
short_url: parseInt(req.params.shorturlno)
}, (err, gotURl) => {
if(err) console.log(err);
if(gotURl == null) {
return res.json({
"error": "No short URL found for the given input"
});
} else {
return res.redirect(gotURl.original_url);
}
});
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
.
Challenge: URL Shortener Microservice
My Repl.it link : https://repl.it/@gauravkr0715/boilerplate-project-urlshortener
Link to the challenge: