Problem with dns.lookup

In the Back End Development and APIs Projects - URL Shortener Microservice | Learn | challenge,
I am using the dns.lookup module in server.js file as"/api/shorturl", function (req, res, next) {
  const url = req.body.url;
  dns.lookup(url, (err, address, family) => {

    if (err) {
      res.json({ error: 'invalid url' });
    let short = shortURLs[-1] ? ++shortURLs[-1]["short_url"] : 1;
    const o = { original_url : url, short_url : short};

The console says address and family parameters are undefined even with I am confused. Please help!

Hi @acoderg

You need to pass the domain name not the full URL. It should be not

You can take a look at the documentation as well.

I hope that helps.

One more ques, are the short urls dynamically generated, like I did, or in some other way and if I am required to use a database?

Well, I have no idea what you are doing in the following line of code.

let short = shortURLs[-1] ? ++shortURLs[-1]["short_url"] : 1;

My guess is you are trying to get the short URL by incrementing an integer each time someone hits that endpoint. I believe it is correct to generate the URL that way.

About using a DB, you need some way of persisting the long URLs and the short ones so that you don’t issue duplicate short URLs and redirect requests accordingly. Yes you need some kind of DB.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.