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 .'/api/shorturl/new', (req,res)=>{
    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' })
             let exist=await Shorturl.findOne({original_url:req.body.url}) 
          original_url: exist.original_url,
          short_url: exist.short_url
             let url={original_url:req.body.url,short_url:randomValue()}

Your project link(s)

If you would like to see in repl :-

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('').hostname
// ''

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