Failing Test 2 & 3 on URL Shortener Microservice Challenge

Hey there!

I’m having some issues with the URL Shortener Microservice challenge. The specific failing tests are #2 and 3, getting a JSON response after POSTing as well as redirecting to the proper site.

I’m not exactly sure what’s wrong. Looking through the process, the first POST request does indeed create a new document into my MongoDB. It also successfully retrieves and redirects using res.redirect(url) when visiting api/shorturl/x, and bounces the invalid ftp:/john-doe.org address with an “invalid URL” JSON response. Regex is culling the HTTP/S and the ending, leaving only the host. Maybe I’m missing something?

Project link: https://replit.com/@raymondwzeng/boilerplate-project-urlshortener-1#server.js

Challenge link: https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/url-shortener-microservice

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

Log your route inputs and outputs and you’ll get

req.body: {"url":"https://boilerplate-project-urlshortener-19.jeremyagray.repl.co/?v=1642955371646"}
req.params: {}
req.query: {}
Incoming URL:  boilerplate-project-urlshortener-19.jeremyagray.repl.co
{
  original_url: 'https://boilerplate-project-urlshortener-19.jeremyagray.repl.co',
  short_url: 1
}
req.body: {"url":"https://boilerplate-project-urlshortener-19.jeremyagray.repl.co/?v=1642955371987"}
req.params: {}
req.query: {}
Incoming URL:  boilerplate-project-urlshortener-19.jeremyagray.repl.co
{
  original_url: 'https://boilerplate-project-urlshortener-19.jeremyagray.repl.co',
  short_url: 1
}
req.body: {}
req.params: {"short_url":"1"}
req.query: {}
Found it:  {
  _id: new ObjectId("61ed81b807594222464889ae"),
  original_url: 'https://boilerplate-project-urlshortener-19.jeremyagray.repl.co',
  short_url: 1
}
req.body: {"url":"ftp:/john-doe.org"}
req.params: {}
req.query: {}
Incoming URL:  ftp:/john-doe.org
No URL found!

The short_url is not changing.

1 Like

Ah! I didn’t realize that we were creating a unique entry for each URL (including the portion following the host).

Thanks for the heads up. I’ve changed the DB entry to use the full original URL rather than the culled version, and also made the count method happen at load rather than on each POST (seems like that was causing some timeout issues). Tests are passing.

A quick followup though - I’m using the following to return the data minus the id:

URLModel.create({original_url: url, short_url: ++documentCount}, (err, data) => {
        if(err != null) {
          return res.json({error: err});
        } else {
          let returnJSON = {original_url: data['original_url'], short_url: data['short_url']};
          return res.json(returnJSON);
        }
      });

It’s a bit ugly to remove the JSON like that by copying the needed values. Is there a better way?

Depends on what you need and what you think is better. This way is explicit and does not affect what is fetched from the database. You can use projections/selections in your find() functions to suppress some fields. You can also write custom toJSON and toObject functions to use as schema options and not have to repeat the code every time you need it.

This is all in the mongoose docs; there may be other ways using the mongodb driver or mongodb directly.