Back End Development and APIs Projects - URL Shortener Microservice

I’ve tested this and all works fine, but am failing the 3rd test still:

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

I’ve looked at other threads and there are moderators showing github links to to actual tests, but these are now all expired pages. How are we supposed to know what the actual test are am I missing something, there is nothing in the readme?

Can any one help please.? I have copied my code below because when I publish the replit and copy link here it never seems to work:

const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const lookup = require('dns-lookup');

// Basic Configuration
const port = process.env.PORT || 3000;

app.use(cors());

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
//app.use(bodyParser.json())


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 arr = [];
let id = 0;
app.post('/api/shorturl', function(req, res, next){
  
  //regex to remove everthing apart from domain
  //const regex = /^[^.]*\./;
  const regexHTTP = /^http|https|www./;
  let url = req.body.url;
  
  //let domain = url.replace(regex, "");
  
  //check if valid url
  //removed the dns lookup as fcc tests didn't like it.  Basic regex for https, http or www. passed the second test
  /*
  lookup(domain, function (err, address, family) {
    console.log("address is:", address);
    console.log("family is:", family);
    if(err){
      res.json({ error: 'invalid url' })
    }else{
      arr.push({
        original_url: url,
        short_url: id
      })
      res.json(arr[id]);
      id++;
    }
  });
  */
  if(!url.match(regexHTTP)){
    
    res.json({ error: 'invalid url' })
  }else{
      arr.push({
        original_url: url,
        short_url: id
      })
      res.json(arr[id]);
      id++;
    }
  next();
})

app.get('/api/shorturl/:number', function(req, res) {
  //change input string to number
  let number = parseInt(req.params.number);
  
  //locate original_url from index of arr
  let foundUrl = arr[number].original_url;
  console.log("foundUrl is:", foundUrl);
/*
  if(foundUrl === "undefined"){
    res.json({ error: 'invalid url' })
  }
  */
  
  //regex to check for pre domain chars
  const regexHTTP1 = /^http|https|www./;
  
  //strip url to domain only
  let strippedUrl = foundUrl.replace(regexHTTP1, "");
  //create new url
  let newUrl = `https://${strippedUrl}`;
  console.log("new url is:", newUrl)
  return res.redirect(newUrl);
});



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



**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36</code>

**Challenge:**  Back End Development and APIs Projects - URL Shortener Microservice

**Link to the challenge:**
https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/url-shortener-microservice

To publish the replit link just use the anchor button in the forum’s edit menu. It looks like a chain icon (to the right side of the I icon)

Hi

Thanks but when I click the anchor link never works for me. It opens the modal, but I am unable to edit the placeholder link. I can’t delete it or type/copy anything into it :-/

Oh. Weird. The other way is to put the link inside double quotes.

Thanks again, I’ll try that here:

boilerplate-project-urlshortener - Node.js Repl - Replit

Your own logged output is telling you the issue:

foundUrl is: https://boilerplate-project-urlshortener.jeremyagray.repl.co/?v=1664112802179
new url is: https://s://boilerplate-project-urlshortener.jeremyagray.repl.co/?v=1664112802179

Note the difference in the two URLs.

In general, you don’t need the code for the tests to get the parameters used, just add

console.log(`req.body: ${JSON.stringify(req.body)}`);
console.log(`req.params: ${JSON.stringify(req.params)}`);
console.log(`req.query: ${JSON.stringify(req.query)}`);

to the top of each route and you’ll get everything provided to the route.

If you just want to see the test code, it’s on github.

Thank you and …oops on the regex!

But I’ve fixed that and now trying:

www.google.com

https://www.facebook.com
http://amazon.com

These all work, but I’m still failing that 3rd test. From the github code I can’t see what they are testing. Are there different url types I should be allowing for other than http:// https:// or www. ?

The last hint before the solutions section on github has the link. The log statements I used earlier will log everything on the repl.it console that the route gets as input, which includes all the tested links.

Thanks again for your help.

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