URL shortener - it seems to work but doesn’t pass


app.use(express.json());
app.use(express.urlencoded({ extended: true }));


app.post('/api/shorturl', function(req, res) {
  var url = req.body.url;
  if (url.includes('https') || url.includes('http')) {
    var u = url.split('/')[2]
  } else {
    return res.json({ error: 'invalid url' })
  }
  dns.lookup(u, async function(err, data) {
    if (err) {
      return res.json({ error: 'invalid url' })
    }
   
    await Website.findOne({original_url: url}).then(async (data) => {
      console.log(data == null )
      if (data == null) {
        await Website.find().then((data) => {
          var short_url = String(data.length + 1)
          var website = new Website({ original_url: url, short_url: short_url })
          website.save().then((data) => {
            res.json({ original_url: url, short_url: Number(short_url) })
          }).catch(error => {
            console.error('Error:', error);
          });
        }).catch(error => {
          console.error('Error:', error);
        });
      } else {
        res.json({original_url: data.original_url, short_url: Number(data.short_url)})
      }
    }).catch(error => {
      console.error('Error:', error);
    });

  })
})

app.get('/api/shorturl/:shorturl?',async function(req, res) {
  var short_url = req.params.shorturl
  if (short_url != undefined) {
    if (!isNaN(Number(short_url))) {
      await Website.findOne({short_url: short_url}).then((data) => {
        if (data == null) {
          return res.json({"error":"No short URL found for the given input"})
        }
        res.redirect(data.original_url)
      }).catch(error => {
        console.error('Error:', error);
      });
    } else {
      return res.json({"error":"Wrong format"})
    }

  } else {
    return res.status(404).json('No URL found')
  }

})

Please provide a Replit or repo so we can more easily test it.

What test doesn’t pass?

second and third test are not passing
thanks in advance

When I run your code on Replit with my own DB is passes the tests.

when i run code on fcc it does not passes the second and third condition idk why?

Where are you running the live code from? Post the link.

You can’t pass this locally, or using a repo, it must be code running online (like on Replit).

i see the fault now i was running it locally thanks for the advice