Back-End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:

Hello, I think FCC has a problem possibly or maybe I am doing something wrong on my end…

on my replit, I tested it and I get this. Isn’t this exactly what test 3 is trying to get? Then how come I fail it when I submit my link to FCC?

###Your project link(s)

githubLink: GitHub - rc072406/boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project.

solution: https://ab27ea1f-3bad-4202-bc89-d3df6f81b1d6-00-2ys3ups27h5s2.picard.replit.dev

Your browser information:

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

Challenge Information:

Back-End Development and APIs Projects - URL Shortener Microservice

Hi @Rc2025

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

/api/shorturl/1 , for example, takes you to the original url.

Happy coding

What’s wrong with my code though?

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

const port = process.env.PORT || 3000;

const urlDatabase = ;
let urlCounter = 1;

app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(‘/public’, express.static(${process.cwd()}/public));

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

app.post(‘/api/shorturl’, (req, res) => {
const originalUrl = req.body.url;

let parsedUrl;
try {
parsedUrl = new URL(originalUrl);
if (parsedUrl.protocol !== ‘http:’ && parsedUrl.protocol !== ‘https:’) {
return res.json({ error: ‘invalid url’ });
}
} catch (err) {
return res.json({ error: ‘invalid url’ });
}

dns.lookup(parsedUrl.hostname, (err) => {
if (err) {

  return res.json({ error: 'invalid url' });
}


let existingEntry = urlDatabase.find(entry => entry.original_url === originalUrl);

if (existingEntry) {
  return res.json({
    original_url: existingEntry.original_url,
    short_url: existingEntry.short_url
  });
}

// Create and save new entry
const newEntry = {
  original_url: originalUrl,
  short_url: urlCounter++
};

urlDatabase.push(newEntry);
res.json(newEntry);

});
});

app.get(‘/api/shorturl/:short_url’, (req, res) => {
const shortUrlParam = req.params.short_url;
const shortUrlId = parseInt(shortUrlParam, 10);

const urlEntry = urlDatabase.find(entry => entry.short_url === shortUrlId);

if (urlEntry) {

return res.redirect(urlEntry.original_url);

} else {
return res.json({ error: ‘No short URL found for the given input’ });
}
});

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

Sorry I don’t know how to add it correctly.

Can someone explain to me what exactly " When you visit

/api/shorturl/<short_url>

, you will be redirected to the original URL." means? Like what does it look like?

When I enter:

https://www.example.net

into the shorturl microservice, I receive the following output:

{"original_url":"https://www.example.net","short_url":23596}

When I type:

https://url-shortener-microservice.freecodecamp.rocks/api/shorturl/23596

into the browser url and press enter, the site https:/www.example.net is loaded in the browser.

Happy coding

The first part works on my end. But I don’t understand the second. Or how to get the second?

Okay the second part works for me.

1 Like

I hope I am not being annoying but is the FCC support taking a break? On my replit I pass all tests but check point 3 won’t pass for me.

this part of the curriculum is archived so it’s low priority, the team is working hard on the new backend material

there is a contributor working on these tests

Okay I see. Thank you for the help and reply