"URL Shortener Microservice" doesn't pass. Please tell me why

Tell us what’s happening:
The system test returns the following errors:

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

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

Well, having said that this has been the hardest challenge faced so far, all my tests say the opposite of what the system says.
I entered many correct links, I get the JSON requested by the challenge in response, and if I send a GET request with the short url produced by my application the browser is redirected to the corresponding long link.
But the tests don’t pass.
Is there a pious soul who can tell me why?

Your project link(s)

solution: https://urlshortener.thornduke.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:85.0) Gecko/20100101 Firefox/85.0.

Challenge: URL Shortener Microservice

Link to the challenge:

I’ve tried entering few urls, and in every case got response: {"error":"invalid url"}. Can you link to your code?

The code is here:


The sharing code is this:


As it can be seen in console, for some reason the dns module is giving error every time new url is tried to be added.

dns.lookup takes a hostname. The hostname param has to just be the hostname and nothing more so for example replit.com and not with the protocol or any subpaths.

Thanks to your advice I almost solved it.
But now the system test gives me the following error:

* If you pass an invalid URL that doesn't follow the
valid http://www.example.com format, the JSON
response will contain { error: 'invalid url' }

Needless to say, I have done a lot of testing and it seems to me that the application works as expected.

I attach the code where the POST message is examined

app.post("/api/shorturl/new", urlEncodedParser, (req, res, next) => {
  try {
    const urlObj = new URL(req.body.url);
  } catch (error) {
    console.error('error: ', req.body.url);
    res.json({error: 'invalid url'})

app.post("/api/shorturl/new", urlEncodedParser, (req, res, next) => {
  req.state = {
    longUrl: '',
    shortUrl: ''
  const url = req.body.url;
  const urlObj = new URL(req.body.url);
  const urlHostName = urlObj.hostname;
  dns.lookup(urlHostName, (err) => {
    if (err) {
      console.error('error: ', [urlHostName, err]);
      res.json({error: 'invalid url'})
    } else {
      if (
        (url.startsWith("https://")) ||
        (url.startsWith("http://")) ||
      ) {
        req.state.longUrl = url;
      } else {
        req.state.longUrl = `http://${url}`;

I am also attaching an invitation to replit, in case anyone wants to take a look at the code.


The ftp protocol shouldn’t be part of a valid URL. You can look at the actual tests as well.