Testing URL shortener API please help!

Tell us what’s happening:
Describe your issue in detail here.

The requirement “When you visit /api/shorturl/<short_url> , you will be redirected to the original URL” is not passing the tests but if I try it in the heroku app it works fine.
I’m not sure if it can be because of the regex i’m using to validate the url string.

const isValidUri = (string) => {
const matchPattern = /^(?:\w+:)?//([^\s.]+.\S{2}|localhost[:?\d])\S$/;
return matchPattern.test(string)
}

Your project link(s)

solution: https://urlshortener12345.herokuapp.com
githubLink: GitHub - gelatinaCosmica/urlshortener: freecodecamp project: url shortener

Your browser information:

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

Challenge: URL Shortener Microservice

Link to the challenge:

Here’s your the structure you are returning:

        const newUrl = new Url({
          short_url: '/api/shorturl/' + suffix,
          original_url: requestedUrl,
          suffix: suffix
        })
        newUrl.save((err, data) => {
          if (err) console.error(err)
          const jsonUrl = {
            // saved: true,
            short_url: data.short_url,
            original_url: data.original_url,
            suffix: data.suffix
          }
          res.json(jsonUrl)

That short_url property is what the tests then use to GET the URL for redirection, not suffix, for which your GET route searches. So I assume the Url.findOne({suffix: ...}) fails when it tries to find a document with a suffix field matching something like /api/shorturl/21 (with 21 just being whatever shortId generated earlier) that was short_url from the POST route. Log the route inputs and ouputs for all your routes to ensure everything is doing what you think it’s doing.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.