Url shoertener not passing tests 2 and 3

Hi! so I’m working on the url shortener project and here’s the code I’m using but i couldn’t understand why it’s not passing these tests altough it’s functional when i directely visit the app:

const express = require('express');
const cors = require('cors');
const app = express();
var bodyParser = require("body-parser");
var dns = require('dns');
app.use(bodyParser.urlencoded({extended: false}));
// Basic Configuration
const port = process.env.PORT || 3000;

app.use(cors());

app.use('/public', express.static(`${process.cwd()}/public`));

app.get('/', function(req, res) {
  res.sendFile(process.cwd() + '/views/index.html');
});

// Your first API endpoint
app.get('/api/hello', function(req, res) {
  res.json({ greeting: 'hello API' });
});

let httpRegex = /^https?:\/\//;
let links = [];
let id=0;

app.post("/api/shorturl", function(req, res) {
	let url = req.body.url;
	let withoutHTTPurl=url.replace(httpRegex, '');
	dns.lookup(withoutHTTPurl, (err) => {
    if(err){
			return res.json({error: 'invalid url'});
		}
		else{
			id++;
			const link={original_url: url, short_url: id};
			links.push(link);
			return res.json(link);
		}
	});
});

app.get("/api/shorturl/:number", function(req, res) {
  let short = req.params.number;
	let link = links.find(l => l.short_url==short);
	if(link){
		res.redirect(link.original_url);
	}
	else{
		res.json({error: 'url not found!'});
	}
});

app.listen(port, function() {
  console.log(`Listening on port ${port}`);
});
  • 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}

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

The argument to dns has to be the hostname without any paths. So only removing the start of the URL isn’t enough.

You can construct a URL and use the hostname property.

new URL('https://forum.freecodecamp.org/t/url-shoertener-not-passing-tests-2-and-3/498985').hostname
// 'forum.freecodecamp.org'

Now that I used the URL constructor, the 2nd and 3rd tests are passing but not the 4th test

It would be nice if you posted a link to a Replit with your code.


Anyway, the “invalid” URL used by the test will pass the dns check. Now in a real app, the check would still be useful but not here.

The only URL used for the test is ftp:/john-doe.org and if you take the hostname part and pass that to dns.lookup it won’t be an error.

const dns = require('dns');
const hostName = new URL('ftp:/john-doe.org').hostname;

dns.lookup(hostName, (err, address, family) => {
  if (err) {
    return res.json({ error: 'invalid url' });
  } else {
    console.log('valid'); // valid
  }
});

The simplest way to pass this specific test is just to check for the ftp protocol. The object the URL constructor returns also has a protocol property.


The fact that the challenge is suggesting to use dns.lookup and then passing a URL that will pass is just odd and I’m pretty sure I have commented on it before. It may be that the redirect that now happens with the john-doe.org domain didn’t always happen.

I should probably open an issue for this (I might have already, I can’t remember).

Here’s the link to my code: https://replit.com/@redouane1/boilerplate-project-urlshortener#server.js

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