[SOLVED]Url Shortener problem reaching else statement

Hello,
I’m working on the URL Shortener project. I have most of it complete but I’m having a problem.
My findUrl is coming back null when it’s not a URL, which it should, but can’t see to get to the ELSE statement. Any suggestions are appreciated.

app.post('/api/shorturl/new', (req, res) => {
  // Regexp for domains
  let urlRegex = /(?:[\w-]+\.)+[\w-]+/;
  let findUrl = urlRegex.exec(req.body.url)[0];
  dns.lookup(findUrl, (err, address, family) => {
    console.log(findUrl);
    if (address) {
       let shortId = randomstring.generate(6);
      let postedUrl = new shortUrl({
        original_url: findUrl,
        short_url: shortId
      });
      postedUrl.save((err, url) => {
        res.json({
          original_url: findUrl,
          short_url: shortId
        });
      })
    } 
    else{
      res.json({
        error: "Are you sure you typed something correctly?"
      });
    }
  })
});

Hi,

I recently finished this project as well, so I thought I’d try to help.

One thing I noticed is that you have installed a dependency called dns which was where I tried to find the dns.lookup method, but could not find any. Searching a little more, I guess Node has a dns module as well? That is where I found the api for lookup. Perhaps this is what’s causing the confusion?

I followed https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback on requiring dns. Yes Node does. The problem I’m having is the findUrl is null when not in a url format and crashing instead of going to the else statement. I’ve tried several ways to pass findUrl but create more problems.

Depending on how dns.lookup handles null affects what if (address) does. (e.g. it might return something other than a falsy value so it doesn’t go the else block). Maybe if (findUrl) would work?

Sorry for the vague responses, I don’t have access to my computer right now and can’t try to run your code.

From researching exec(), it returns an array and true or false. Upon checking it with the regex, if the input wasn’t in the regex check format or false, it returned a null value and let it through if it was true. I changed it to .test(). declared a new variable for the user input, tested that in the findUrl then in the if statement, checked if it was true and made the callback an anonymous function. That seemed to fix it. Thank you for your suggestions.

app.post('/api/shorturl/new/', (req, res) => {
  
// Regexp for domains
  let userUrl = req.body.url;
  let formatRegex =/(?:[\w-]+\.)+[\w-]+/;
  let findUrl = formatRegex.test(userUrl);

  dns.lookup(userUrl, () => {

    let shortId = randomstring.generate(6);//generates 6 digit id for short url.

    if (findUrl === true) {
      let postedUrl = new shortUrl({//user url
        original_url: userUrl,
        short_url: shortId
      });
      postedUrl.save((err, url) => { //saves to database
        res.json({
          original_url: userUrl,
          short_url: shortId
        });
      });
    } else{
      res.json({
        error: "Invalid Url"
      });
    }
  });
});