when i test the code in freecodecamp it gives me the
When you visit /api/shorturl/<short_url> , you will be redirected to the original URL
here is my code I don’t know what’s wrong it works just fine
app.post('/api/shorturl', function (req, res) {
const urlRegex = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
const url = req.body.url;
if (url.match(urlRegex)) {
num++;
finalData.push({short_url: num, original_url: url});
res.json({ original_url: url, short_url: num});
} else {
res.json({ error: 'invalid URL' });
}
});
app.get('/api/shorturl/:short_url', (req, res) => {
const short = req.params.short_url;
for (let i = 0; i < finalData.length; i++) {
console.log(finalData[i].short_url, short);
if (finalData[i].short_url === short) {
res.redirect(finalData[i].original_url);
} else {
res.json({ error: 'invalid URL' });
}
}
});
Your project link(s)
solution: https://replit.com/@abd-el-rahmane7/boilerplate-project-urlshortener
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
Challenge: URL Shortener Microservice
Link to the challenge: