Need help url shortner microservice

Here is my code for post request in url shortner microservice .It works fine in rel repository but when i paste the repl link to check in solution link then i could not pass the test .

app.post('/api/shorturl/new', (req,res)=>{
     console.log(req.body.url)
    let fs=/\//ig
    // regex condition t oremove https from url
    let regex= /^(https)\:\/\//ig
    // replacing https to empty string 
    let host =req.body.url.replace(regex,'').split('/')

    // // handling the invalid urlusing dns inbuilt module 
    dns.lookup(host[0],async (err, address, family)=>{
      if(err || host.length == 0 ) {
      return  res.json({ error: 'invalid url' })
      }
    
      else{
             let exist=await Shorturl.findOne({original_url:req.body.url}) 
        if(exist){
              res.json({
          original_url: exist.original_url,
          short_url: exist.short_url
        })}
           else{
             let url={original_url:req.body.url,short_url:randomValue()}
        createUrl(url)
      res.json(url)}
      }
    })
  
  })

Your project link(s)

If you would like to see in repl :-https://replit.com/@alamsameer/Shortner-url#server.js

The invalid test URL is using the FTP protocol so your regex won’t work. Well it sort of does, but then the hostname is at index 2 edit: scratch that, the URL only has one / so it would be index 1

You can use the hostname property on the URL object you get back from the URL constructor.

new URL('ftp://forum.freecodecamp.org/t/need-help-url-shortner-microservice/501031').hostname
// 'forum.freecodecamp.org'

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