Problem with dns.lookup

In the Back End Development and APIs Projects - URL Shortener Microservice | Learn | freeCodeCamp.org challenge,
I am using the dns.lookup module in server.js file as

app.post("/api/shorturl", function (req, res, next) {
  const url = req.body.url;
  dns.lookup(url, (err, address, family) => {
    console.log(err); 
    console.log(address);
    console.log(family);

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

The console says address and family parameters are undefined even with https://www.freecodecamp.org/. I am confused. Please help!

Hi @acoderg

You need to pass the domain name not the full URL. It should be freecodecamp.org not https://www.freecodecamp.org/.

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.