Tell us what’s happening:
Test 2 and 3 dont pass, but code works fine:
const originalUrls = [];
const shortUrls = [];
app.post('/api/shorturl', (req,res)=>{
const url = req.body.url;
const foundIndex = originalUrls.indexOf(url)
if(!url.includes('https://') && !url.includes("https://")){
return res.json({error:'invalid url'})
}
if(foundIndex<0){
originalUrls.push(url)
shortUrls.push(shortUrls.length)
return res.json({
original_url: url,
short_url: shortUrls.length-1
})
}
return res.json({
original_url: url,
short_url: shortUrls[foundIndex]
})
})
app.get("/api/shorturl/:shorturl",(req, res)=>{
const shorturl = parseInt(req.params.shorturl);
const foundIndex = shortUrls.indexOf(shorturl)
if(foundIndex<0){
return res.json({
"error": "No short URL found for the given input"
})
}
res.redirect(originalUrls[foundIndex])
})
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Back End Development and APIs Projects - URL Shortener Microservice