URL Shortener seems to work but tests fail

Hi there, so I have been trying to solve this problem on my own for a couple of hours, even though everything seems to work just like in the description my code fails all of the tests.

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
const mongoose = require('mongoose');
const mongoDB = process.env.DB_URI;
const { Schema } = mongoose;
const bodyParser = require('body-parser');
const dns = require('dns');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const urlSchema = new Schema({
  url: String,
  id: mongoose.ObjectId,
  number: Number
});
const urlModel = mongoose.model("urls", urlSchema);

mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });


// 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' });
});

app.post('/api/shorturl/new', function(req, res) {
  let rand = Math.floor(Math.random() * 10000);
  let shortNew = new urlModel({
    url: req.body.url,
    number: rand
  });
  const woprotocol = req.body.url;
  const regex = /^https?:\/\//i
  if(regex.test(woprotocol)) {

const address = woprotocol.replace(regex, '');
  dns.lookup(address, { all: true }, function(error, addresses) {
    if(error) {
      console.error(error);
    } else {
      let url;
      shortNew.save();
    res.json({original_url: woprotocol, short_url: rand})
    }
  });
  
} else {
  res.json({error: "invalid URL"});
}
}
);

app.get('/api/shorturl/:number', function(req, res) {
  urlModel.findOne({ number: req.params.number }, function (err, docs) {
    if(err) {console.log(err);}
    else {
      res.redirect(docs.url);
    }
  });

});

app.listen(port, function() {
  console.log(`Listening on port ${port}`);
});

Thanks for any kind of suggestion in advance!

Hello there,

This is a common mistake. It has to do with the dns.lookup function. The first argument must be only a domain. There are quite a few threads on this forum about it, if you get stuck.

The tests for this project are here (you can find the URLs used to test): freeCodeCamp/url-shortener-microservice.md at main · freeCodeCamp/freeCodeCamp · GitHub

Otherwise, would you mind sharing a link to your app and project code?

Hope this helps

1 Like

Hey, thanks for the answer. Do you mean skipping the protocol? If yes, then I have already done it using regex. It is in this part of the code:

const woprotocol = req.body.url;
  const regex = /^https?:\/\//i
  if(regex.test(woprotocol)) {

const address = woprotocol.replace(regex, '');
  dns.lookup(address, { all: true }, function(error, addresses) {
    if(error) {
      console.error(error);
    } else {
      shortNew.save();
    res.json({original_url: woprotocol, short_url: rand})
    }
  });

EDIT: I have got rid of the dns.lookup part and now it works!

Anyway, thank you once again.

Glad you got it working.

The issue is dns.lookup expects just the domain. That is:

  • This works: freecodecamp.org
  • This does NOT: freecodecamp.org/something/something-else
1 Like

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