URL shortener : Challenge

Question

Can anyone please describe the requirements here?
Like, Can we create a schema and save the actual URL and the shortened URL.
SO that every time we visit the shortened URL, we get the original URL corresponding to the short URL and redirect the user?

Also, it would be great if anyone can point the use of the dns module

Thank You :smiley:

Your browser information:

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

Challenge: URL Shortener Microservice

Link to the challenge:

I guess the short answer to your first question is, yes. And you can look at the Node docs for the second.

BTW, as it stands now the only thing you have to do to validate the URL is to make sure it is not a URL using the ftp protocol. But you can expand this to check for valid constructed URLs and even create a link checker to see if the resource the link is pointing to is still live.

1 Like

Hey @lasjorg thank you :hugs:

So far, I have passed 3 tests and failed the last one.

The one I failed is :
If you pass an invalid URL that doesn't follow the valid http://www.example.com format, the JSON response will contain { error: 'invalid url' }

But I am handling that case as well.
When I am posting any invalid URL I am getting the desired response in JSON. Which is error : "invalid url"
Here is my code block, which includes:

  • checking for URL
  • if valid go ahead and save
  • else throw the error response
    The snippet:

try{
    let url = new URL(req.body.url)
    let originalUrl = req.body.url;
    let shortUrl = Math.floor(Math.random() * 1000);
    console.log('req recvd, processing')
        
        // saving url
        let savedUrl = await new Model({
          originalUrl,
          shortUrl
        }).save()
        console.log(savedUrl)
     
  

     res.json({
       "original_url" : originalUrl,
       "short_url" : shortUrl
     })
    
    }
  catch(e){
   // if invalid url
   // typeerror is returned
   // check instance of typeerrror

  if(e instanceof TypeError) res.json({
    "error" : "invalid url"
  })
 }

Below is the screenshot of the response in JSON- when I post abc[dot]com


Do I have to use the DNS core module exclusively for URL validation?

Additionally, for more context you can find the entire code here in my repo:
fcc-crisp-url

Update:

  • I even tried the core DNS module,
    Even though I am getting the desired response as before, that is error : invalid URL on passing invalid URL

  • still, that test won’t pass :lying_face::lying_face:

I didn’t test your code but I’m pretty sure it won’t throw on the invalid URL you are getting from the test which is ftp:/john-doe.org

You can see all the tests here.

As I said, one option is just to look at the protocol and respond with the invalid URL object if it is a URL using ftp.

new URL('ftp:/john-doe.org').protocol;
// 'ftp:'

Edit: BTW, your /api/shorturl/:short GET route is nested inside the /api/shorturl POST route handler. That seems like a mistake.

1 Like

passed all the tests finally :pleading_face::kissing_heart:

Thank you soo much @lasjorg :hugs::hugs:

checking for ftp: worked like magic <3

can you pl. elaborate a little on your EDIT? I am sorry I don’t get it.
or better yet, create an Issue for the same, in the repo? :hugs:

1 Like

I add three comments so it is easier to spot.

app.post("/api/shorturl", async (req, res) => { // handler start
  console.log("hmm");
  try {
    let url = new URL(req.body.url);
    let originalUrl = req.body.url;

    let shortUrl = Math.floor(Math.random() * 1000);
    console.log("req recvd, processing");

    // ! if url uses ftp protocol, throw error response
    if (url.protocol === "ftp:") {
      res.json({
        error: "invalid url",
      });
    } else {
      // saving url
      let savedUrl = await new Model({
        originalUrl,
        shortUrl,
      }).save();
      console.log(savedUrl);

      res.json({
        original_url: originalUrl,
        short_url: shortUrl,
      });
    }
  } catch (e) {
    // if invalid url
    // typeerror is returned
    // check instance of typeerrror

    if (e instanceof TypeError)
      res.json({
        error: "invalid url",
      });
  }

  // route nested inside /api/shorturl handler
  app.get("/api/shorturl/:short", async (req, res) => {
    let short = req.params.short;

    let search = await Model.findOne({ shortUrl: short }).exec();
    console.log("search rsult", search);
    res.redirect(search.originalUrl);
  });
}); // handler end

ahhhhaaaa - Thank You so much!

Really appreciate the patience, and feedbacks :blue_heart::blue_heart:

I just pushed the new changes ^-^"


1 Like