URL Shortener Test is working but test case does not pass

The below test case does not pass even though when I navigate to the shortened URL, the redirect is happening correctly.

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

My only thought is that if I navigate to a second URL, the first URL persists and is not overwritten with a second POST request.

solution: https://project-urlshortener.justinpaul2.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36.

Challenge: URL Shortener Microservice

Link to the challenge:

Could you link to your code as well?

require(‘dotenv’).config();
const express = require(‘express’);
const cors = require(‘cors’);
const app = express();

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

app.use(cors());

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

let myURL = “”;

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

});

app.post("/api/shorturl/new", (req, res) => {
let finalURL = ‘’;

req.on(‘data’,(chunk) => {

myURL += chunk;
finalQuery = decodeURIComponent(myURL);
//remove the url2= string to result in the full URL
finalURL = finalQuery.split("=").pop();
console.log(finalURL);
regexMatch = /^http/;
var finalMatch = regexMatch.test(finalURL);
if(finalMatch){
  res.json({ "original_url": finalURL, "short_url": 90 });
} else {
  res.json({ "error": "invalid url" });
}

})

app.get("/api/shorturl/90", (req, res) => {
res.redirect(307,finalURL);

});

});

// app.get("/api/shorturl/90", (req, res) => {
// res.redirect(307,finalURL);

// });

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

Apologies, I am new to the forum. I wasn’t sure the best way to provide a link to my server.js. Here is a link from repl.it

https://repl.it/@JustinPaul2/project-urlshortener#server.js

I now understand your first post. Yes, using the same short url cannot redirect to two different pages at the same time. Why hard-code that number anyway?

I hardcoded the number in because I was unclear on the instructions from the FCC challenge page. As I understood it, I could choose any arbitrary number as long as when that number gets used in my URL, the redirect happens successfully. How does that number get generated if I don’t choose it myself?

Well, your code can generate it one way or another for each entered url. So it can be used with multiple urls and each shortened url will redirect to correct page.

1 Like

Understood, thank you