# URL Shortener Question

**URL:** <https://forum.freecodecamp.org/t/url-shortener-question/432733>\
**Category:** I Built This\
**Created:** [November 28, 2020, 7:11pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733 "2020-11-28T19:11:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![julio-pinheiroo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/julio-pinheiroo/32/167491_2.png) [@julio-pinheiroo](https://forum.freecodecamp.org/u/julio-pinheiroo)\
**Post date:** [November 28, 2020, 7:11pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/1 "2020-11-28T19:11:52Z")

</div>

Hi @skaparate!

I am having the same issue on my project, it works but not passing on the test…  
Can you please see what is going on? I would be glad. 😄

> **[URL Shortener Microservice | freeCodeCamp.org](https://boilerplate-project-urlshortener-6.juliano988.repl.co)**

[juliano988/boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project. (github.com)](https://github.com/juliano988/boilerplate-project-urlshortener)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 28, 2020, 7:13pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/2 "2020-11-28T19:13:25Z")

</div>

I have moved your post to a new thread.

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [November 28, 2020, 7:39pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/3 "2020-11-28T19:39:52Z")

</div>

Hello!

Your problem is how you’re validating the URL.

[The first problem is that you expect that every URL end with /](https://github.com/juliano988/boilerplate-project-urlshortener/blob/e2d2e9625e3e4bd4a8d46737f310e52db00e1adc/server.js#L62) which would invalidate valid ones:

```auto
app.post('/api/shorturl/new', function(req, res) {
    if ((/^(http|https):\/\/.+/).test(req.body.link)) {

```

Test your validator in isolation for the following (not exhaustive, but valid) URLs:

```javascript
[
    'http://freecodecamp.org',
    'http://freecodecamp.org/',
    'http://www.freecodecamp.org',
    'http://www.freecodecamp.org/',
    'https://freecodecamp.org',
    'https://freecodecamp.org/',
    'https://www.freecodecamp.org',
    'https://www.freecodecamp.org/',
    'http://freecodecamp.org/path',
    'http://freecodecamp.org/path/',
    'http://freecodecamp.org/path?a=1',
    'http://freecodecamp.org/path?a=1&b=2'
]

```

All of those are valid, plus you should also expect other protocols, like `ftp`: `ftp://domain.tld/etc`.

If you don’t want to come with your own solution, your could use the URL API of nodejs: [https://nodejs.org/api/url.html](https://nodejs.org/api/url.html).

---

<div class="post-metadata">

**Author:** ![julio-pinheiroo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/julio-pinheiroo/32/167491_2.png) [@julio-pinheiroo](https://forum.freecodecamp.org/u/julio-pinheiroo)\
**Post date:** [November 29, 2020, 4:01pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/4 "2020-11-29T16:01:46Z")

</div>

Hi again @skaparate!

> [@skaparate](#):
>
> Your problem is how you’re validating the URL.

I think that is not actually the problem, I have tried a lot of different ways to validate the URL but the problem still persists…

And just to make sure, I have searched on the internet this project to check if they were going to pass or not, but no one did…  
The only one which actually passed was the example one.

At this point I really don’t know what’s else can I do to pass.

Are you sure that is nothing wrong with the test? This project shouldn’t be that hard.

[URL Shortener Microservice | freeCodeCamp.org (juliano988.repl.co)](https://boilerplate-project-urlshortener-9.juliano988.repl.co/)

[juliano988/boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project. (github.com)](https://github.com/juliano988/boilerplate-project-urlshortener)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 29, 2020, 4:14pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/5 "2020-11-29T16:14:37Z")

</div>

Other people have passed this project, so I don’t think the tests are seriously flawed. The same fix was needed in the other topic you found.

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [November 29, 2020, 6:27pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/6 "2020-11-29T18:27:33Z")

</div>

I’m not saying that’s the only problem, but it’s the main one 🙂.

One of the tests sends the following **valid URL** : _[https://timestamp-microservice.freecodecamp.rocks/api/timestamp/1606674081372](https://timestamp-microservice.freecodecamp.rocks/api/timestamp/1606674081372)_. Your project responds with the following (bottom right of the image, says `error: "invalid url"`):

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/a/9/a9e276a1fe2b0d2463520ed77cd0df65d60e97d7.png)

The same happens with another test using a valid URL, so, the main problem is how you’re validating the URLs 🙂.

---

<div class="post-metadata">

**Author:** ![kravmaguy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kravmaguy/32/179611_2.png) [@kravmaguy](https://forum.freecodecamp.org/u/kravmaguy)\
**Post date:** [February 25, 2021, 6:00am UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/7 "2021-02-25T06:00:37Z")

</div>

Im having a similar problem as the OP. Id like to reference the advice I found on this thread, which unfortunately did not work for me:

> [@skaparate](#):
>
> ```auto
> [
> 'http://freecodecamp.org',
> 'http://freecodecamp.org/',
> 'http://www.freecodecamp.org',
> 'http://www.freecodecamp.org/',
> 'https://freecodecamp.org',
> 'https://freecodecamp.org/',
> 'https://www.freecodecamp.org',
> 'https://www.freecodecamp.org/',
> 'http://freecodecamp.org/path',
> 'http://freecodecamp.org/path/',
> 'http://freecodecamp.org/path?a=1',
> 'http://freecodecamp.org/path?a=1&b=2'
> ]
> 
> ```

Ive tested all of these, and it is redirecting as I would expect, yet the project is still not passing the test:

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

[repl link](https://repl.it/@greykrav/boilerplate-project-urlshortener#README.md)

I believe there is a section after this which deals with software testing… Since Ive not advanced to the next section yet, Im not sure how to interpret this test, in order to resolve the problem:

```js
async (getUserInput) => {
  const url = getUserInput('url');
  const urlVariable = Date.now();
  let shortenedUrlVariable;
  const postResponse = await fetch(url + '/api/shorturl/new/', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: `url=https://timestamp-microservice.freecodecamp.rocks/api/timestamp/${urlVariable}`
  });
  if (postResponse.ok) {
    const { short_url } = await postResponse.json();
    shortenedUrlVariable = short_url;
  } else {
    throw new Error(`${postResponse.status} ${postResponse.statusText}`);
  }
  const getResponse = await fetch(
    url + '/api/shorturl/' + shortenedUrlVariable
  );
  if (getResponse) {
    const { redirected, url } = getResponse;
    assert.isTrue(redirected);
    assert.strictEqual(
      url,
      `https://timestamp-microservice.freecodecamp.rocks/api/timestamp/${urlVariable}`
    );
  } else {
    throw new Error(`${getResponse.status} ${getResponse.statusText}`);
  }
};

```

```auto

```

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [February 25, 2021, 2:21pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/8 "2021-02-25T14:21:34Z")

</div>

Your project passes for me. Maybe you fixed before I tested?

---

<div class="post-metadata">

**Author:** ![kravmaguy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kravmaguy/32/179611_2.png) [@kravmaguy](https://forum.freecodecamp.org/u/kravmaguy)\
**Post date:** [February 26, 2021, 8:27am UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/9 "2021-02-26T08:27:35Z")

</div>

negative, I did not change anything.

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [February 26, 2021, 12:55pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/10 "2021-02-26T12:55:47Z")

</div>

To be clear then, it still doesn’t pass the tests? If so, it doesn’t pass any tests or just the one that you posted in your last message?

Could you try using a different browser and/or disabling all plugins/extensions from your browser?

---

<div class="post-metadata">

**Author:** ![kravmaguy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kravmaguy/32/179611_2.png) [@kravmaguy](https://forum.freecodecamp.org/u/kravmaguy)\
**Post date:** [February 27, 2021, 12:53am UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/11 "2021-02-27T00:53:14Z")

</div>

Yes sir, It passes. I was replying to your question asking if i had changed anything about it since you tested it.

I was trying to get it work for hours but then i saw this:

> [@Demo projects hosted at freeCodeCamp.rocks are down](https://forum.freecodecamp.org/t/demo-projects-hosted-at-freecodecamp-rocks-are-down/448293):
>
> Tell us what’s happening: It looks like freecodecamp.rocks is down, so it’s not possible to pass the tests on “URL Shortener Microservice” because they depend on that server being alive. (I could be wrong, but regardless, that site shouldn’t be down as it hosts the example projects.) Test 3 fails because the redirect to the following URL returns an error: [https://timestamp-microservice.freecodecamp.rocks/api/timestamp/1614221346795](https://timestamp-microservice.freecodecamp.rocks/api/timestamp/1614221346795) Your project link(s) [https://repl.it/@sethipandi/…](https://repl.it/@sethipandi/freeCodeCamp-URL-Shortener-Microservice)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 1:08pm UTC](https://forum.freecodecamp.org/t/url-shortener-question/432733/12 "2021-06-01T13:08:01Z")

</div>

Closed on June 1, 2021, 1:08pm UTC


