Tell us what’s happening:
Hello! I’ve been working on the URL Shortener challenge and I am stumped on how to pass the last test -
If you pass an invalid URL that doesn’t follow the valid
http://www.example.com
format, the JSON response will contain{ error: 'invalid url' }
Let me know if there is an issue with how I am formatting the JSON response or if the dns.lookup callback function is missing something. I’m stumped here!
Your code so far
app.post('/api/shorturl/new', (req, res, next) => {
const originalURL = req.body.url;
const urlObject = new URL(originalURL);
console.log(urlObject)
dns.lookup(urlObject.hostname, (err, address, family) => {
if (err) {
res.json({ error: 'invalid url' });
} else {
let shortenedURL = Math.floor(Math.random()*100000).toString();
// create an object(document) and save it on the DB
let data = new Url({
url: originalURL,
shortuUrl: shortenedURL
});
data.save((err, data) => {
if (err) {
console.error(err);
}
});
res.json({
original_url: originalURL,
short_url: shortenedURL
})
};
});
});
Link to project: boilerplate-project-urlshortener - Replit
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36
.
Challenge: URL Shortener Microservice
Link to the challenge: