Hi everyone,
I’m solving the URL Shortener Microsevice challenge and I can’t seem to to pass the invalid url challenge. I’m using the valid-url package to check if it is a valid uri. I’ll leave the relevant code and output below. Any hints will be appreciated. Thanks!
app.post("/api/shorturl/", async function(req, res) {
let url = req.body.url;
if (!url) {
return res.json({
error: "invalid url"
})
}
let urlCode = shortId.generate();
if (!(validURL.isUri(url))) {
return res.json({
error: "invalid url"
})
}
else {
try {
let one;
one = await URL.findOne({ original_url: url });
if (one) {
return res.json({
'original_url': one.original_url,
'short_url': one.short_url
})
}
else {
one = new URL({
'original_url': url,
'short_url': urlCode
})
await one.save();
return res.json({
'original_url': one.original_url,
'short_url': one.short_url
});
}
} catch (err) {
return res.json({ error: 'invalid url' })
}
}
})
I provided an Invalid URL to debug the code and here is the output for this url : “stackoverflow.com”