Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:
I am gestting Cannot POST /api/shorturl

Your project link(s)
https://boilerplate-project-urlshortener.kritostar.repl.co/
solution: boilerplate-project-urlshortener - Replit

Your browser information:

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

Challenge: Back End Development and APIs Projects - URL Shortener Microservice

Link to the challenge:

You are supposed to set up a POST route at /api/shorturl. Instead you have set up a POST route to /api/shorturl/new. Once you fix that you will find you have not defined a variable you are using on line 42. You have another only line 42.

I have just corrected the code, and the redirect and find is working but the challenge is showing me X

When I submit a new url, I expect to get a JSON reponse containing original_url and short_url properties. With the way you have written your code, the only time I will get such a response, is if I submit a previously submitted (and saved url).

The issue is in your current logic (shown below):

      if (!docs) {
        var nuevo = new Direccion({
          original_url: url
        });
        console.log('about to save')
        nuevo.save();

      }
      else {
        res.json({
          original_url: docs.original_url,
          short_url: docs.short_url
        })
      }

You only return a JSON response if docs is not null which means if the findOne method actually finds an existing url.

Yeap, I dont know how to save and show the saved one :S

Sure you do. You return a JSON response in your else code block.

yes but if its a new one, I am saving and that’s it, I am not showing it

But according to the project instructions, if you submit a valid url, you must return the applicable JSON response. The instructions do not say “Only return a JSON response if the url submitted was previously submitted and saved.”

Sorry to keep on bothering you. My code is now working, but the tests are not passing. Can you see maybe why? Thanks a lot

You modified the index.html file and changed the name attribute from url to input_url. The tests are expecting to post using a name of url, so if you change it back, then you will pass the first 3 tests.

The last test is failing because the tests are not expecting to receive a 401 status. Just return the required JSON object without changing the status and you will pass it.

1 Like