I have a topic 3 from the URL Shortener Microservice test that I am not able to solve, and it is the only one that presents this error, I checked it in the browser console and it presents the following error:
My code:
app.post("/api/shorturl", async function(req, res){
const url = req.body.url;
const urlCode = shortid.generate();
console.log(urlCode)
if(!validUrl.isWebUri(url)){
res.json({error: "invalid url"});
}else{
try{
var findOne = await URL.findOne({
original_url: url
});
if(findOne){
res.json({original_url: findOne.original_url,
shorturl: findOne.shorturl})
}else{
findOne = new URL({
original_url: url,
shorturl: urlCode
})
await findOne.save();
res.json({
original_url: findOne.original_url,
shorturl: findOne.shorturl
})
}
} catch (err){
console.error(err)
}
}
})
app.get("/api/shorturl/:shorturl?", async function(req, res){
try{
const urlParams = await URL.findOne({
shorturl: req.params.shorturl
})
if(urlParams){
return res.redirect(urlParams.original_url);
}
console.log(req.params.shorturl);
}catch (err){
console.log(err)
}
})
My project: https://boilerplate-project-urlshortener.thijv7.repl.co/