Automatic tests fail, but my solution seems to work well, can someone review my code ?
app.post('/api/shorturl', (req, res) => {
const { url } = req.body;
const tmpUrl = url.replace(/^https?:\/\//, '');
dns.lookup(tmpUrl, (err, address) => {
if (err)
res.json({
error: 'invalid url'
});
else {
const tmp = {
original_url: url,
short_url: urls.length + 1,
};
urls.push(tmp)
let { original_url, short_url } = tmp;
res.json({
original_url,
short_url
});
}
});
});
app.get(’/api/shorturl/:shorturl’, (req, res) => {
let { shorturl } = req.params;
shorturl = Number(shorturl);
const urlObject = findUrl(shorturl);
if (urlObject) {
res.redirect(urlObject.original_url);
}
else {
res.json({
error: ‘invalid url’
});
}
});
Challenge: URL Shortener Microservice
Link to the challenge: