URL Shortener Microservice - Why my project is not passing tests?

Hello. Can anyone help me to understand why my project is not passing the tests?
My project is failing two of the tests but I can manually confirm it actually should pass every listed test.

1 - You can POST a URL to /api/shorturl and get a JSON response with original_url and short_url properties. Here’s an example: { original_url : 'https://freeCodeCamp.org', short_url : 1}

2 - When you visit /api/shorturl/<short_url> , you will be redirected to the original URL.

This is my project live demo:

I have no remote repository yet for this project but there is a pastebin of the server.js file in case it can help on find a solution: require('dotenv').config();const MONGO_URI = process.env['MONGO_URI'];const - Pastebin.com

Thanks in advance.

What’s the use of '' after the + sign here?

(parseInt(latestUrl[0].short_url) + 1) + ‘’

In that case your short_url is no longer an integer but a string.

See this one.

1 Like

Hello. edper. I tryed with the short_url property as integer before but it doesn’t pass those two tests neither so I changed it into string as you see. Integer or String both get the same two tests to fail. Thanks the feedback :smiley:

If there is no save url yet in your Mongodb you still have a string with this:

let newShortUrl = ‘1’

In line with that you might want to clean up your database first before testing it again.

Also, I suspect your third party urlValidator.js could also be a problem here. In my case I just used pure regex.

Also, for post method I tested if the pass parameter is a number.

For example they can pass a legal one like this:


or an illegal one like this:


1 Like

Your code here

app.post('/api/shorturl', async (req, res) => {
  const url = req.body.url
  // const validation = urlValidator(url)
  // if (!validation) return res.send({ error: 'invalid url' })
  const isRepeateUrl = await Url.findOne({ original_url: url })
  if (isRepeateUrl) return res.json({
    original_url: isRepeateUrl.original_url,
    short_url: isRepeateUrl.short_url
  const latestUrl = await Url.find({}).sort({ short_url: 'desc' }).limit(1)
  let newShortUrl = '1'
  if (latestUrl[0]) newShortUrl = (parseInt(latestUrl[0].short_url) + 1) + ''
  console.log(`shortUrl: ${newShortUrl}`);


shortUrl: NaN

which is what gets logged in your GET route. This code looks like it may only work on repeated URLs and fail on new ones. You can use the automatically generated and added _id` field as your shortened URL.

Can’t check the validation without that file. It’s much easier to put this in a repl on repl.it and post a link.

1 Like

Hello and thanks both for the great feedback :clap:

The validator is a basic regex I made by myself.

const urlRegex = /^https?\:\/\/([a-zA-Z0-9]+\.)?[a-zA-Z0-9]+\.[a-zA-Z0-9]+\/?[\w\/\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@\%]+?$/

function urlValidator (url) {
  const validation = urlRegex.test(url)
  return validation

module.exports = urlValidator

I also uploaded the code into a replit: https://replit.com/@JoseAlfu/boilerplate-project-urlshortener#server.js

I checked your code and it works 100% when I used my own regex. So, your code is good already except for your regex here. So, if you can find the regex that will work on this, you will be fine.

1 Like

I changed my custom regex and it works!!! Thanks.

My custom regex was too much strict taking into account the given example alone:

{ original_url : 'https://freeCodeCamp.org', short_url : 1}

Then I checked every input of the tests and found a valid url format my regex wasn’t passing:


Once my regex passes the above url then all tests were success.

Thanks once again everyone.

The challenge does not specifically require a number for a short url I believe. I used the nanoid package to generate mine. If anyone is reading this and curious about generating short urls.

I think you are right here. Thanks for the correction.