I'm not able to pass the tests even when my project is working fine. I'm stuck here with no clue

Tell us what’s happening:
I have completed the project (from my side) and ran it to check it. It works completely fine for all my tests, yet when I submit it, it fails 2 out of the 4 tests and I have no idea why. I’m stuck on this for a while now and would really like some help

Your code so far These are the 2 endpoints we have to work upon:


app.post('/api/shorturl/new', async (req, res) => {
  var count;

  try {
    const URLToShorten = await new URL(req.body.url);
    console.log(URLToShorten);

    if(URLToShorten.protocol != "http:" && URLToShorten.protocol != "https:") {
      return res.json({
        "error": "Invalid URL"
      });
    }

    const foundURL = await ShortURL.findOne({
      original_url: URLToShorten.origin
    });

    if(foundURL)
      return res.json({
        original_url: foundURL.original_url,
        short_url: foundURL.short_url
      });
    else {

      await dns.lookup(URLToShorten.host, (err, data) => {
        if(err) console.log("error is: " + err);
        else console.log("data is: " + data);  
      });

      await ShortURL.countDocuments({}, (err, data) => {
        if(err) console.log(err);
        else count = data;
      });

      const newURL = new ShortURL({
        original_url: URLToShorten.origin,
        short_url: count+1
      });

      newURL.save((err, savedURL) => {
        if(err) console.log(err); 
        return res.json({
          original_url: savedURL.original_url,
          short_url: savedURL.short_url
        });
      }); 

    }

  } catch(err) {
    res.json({
      "error": "Invalid URL"
    }); 
  }
 
}); 

app.get('/api/shorturl/:shorturlno', (req, res) => {
  ShortURL.findOne({
    short_url: parseInt(req.params.shorturlno)
  }, (err, gotURl) => {
    if(err) console.log(err);
    
    if(gotURl == null) {
      return res.json({
        "error": "No short URL found for the given input"
      });
    } else {
      return res.redirect(gotURl.original_url);
    }

  });
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.

Challenge: URL Shortener Microservice

My Repl.it link : https://repl.it/@gauravkr0715/boilerplate-project-urlshortener

Link to the challenge:

check that you are using the most recent boilerplate, there is an ongoing update of the projects

also please post your project link


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I’ve added the link to my Repl.it project in the post