Hi guys, I work on URL shortener project and am stucked. The thing I cant figure out is how to validate URL address. We are suposed to use dns.lookup(). When I use it separately it works. When I use it within POST method it always figures out that a URL is invalid, even though I post a valid one.
Here is the project on glitch: https://glitch.com/~url-shortener-lm
Here is the code:
app.post('/api/shorturl/new', (req, res, next) => {
var originalURL = req.body.url;
dns.lookup(originalURL, (err, address, family) => {
if (err) {
res.json({
originalURL: originalURL,
shortenedURL: "Invalid URL"
});
} else {
var shortenedURL = Math.floor(Math.random()*100000).toString();
var data = new Model({
originalURL: originalURL,
shortenedURL: shortenedURL
});
data.save(function(err, data) {
if (err) {
return console.error(err);
}
});
return res.json({
originalURL: originalURL,
shortenedURL: shortenedURL
})
};
});
});
Can anyone please help?
Thanks a lot!