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?
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:
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.