Tell us what’s happening:
Title: URL Shortener Microservice – Test 3 fails even though redirect works
Title: URL Shortener Microservice – Test 3 fails even though redirect works
Hello everyone,
I’m currently working on the URL Shortener Microservice project from the Back End Development and APIs certification.
My application seems to work correctly:
I can POST a URL to /api/shorturl
The API returns the correct JSON response with original_url and short_url
When I visit /api/shorturl/1, the app correctly redirects to the original URL
However, FreeCodeCamp test #3 still fails :
When you visit /api/shorturl/<short_url>, you will be redirected to the original URL.
I tested the redirect directly in the browser and it works as expected.
Project links:
Live project:
https://proyecto2-1e09.onrender.com
Source code:
https://github.com/gerardojaracarrasco-droid/proyecto2
Example:
POST request to:
/api/shorturl
Body:
url=https://google.com
Response:
{
"original_url": "https://google.com",
"short_url": 1
}
Visiting:
/api/shorturl/1
Correctly redirects to https://google.com .
Despite this, the FreeCodeCamp test still marks test #3 as failed.
Has anyone experienced the same issue or knows if this might be a problem with the test runner?
Thanks in advance!
sometimes the test will log an error to the console in the browser when it runs.
Can you check and see if such a thing is getting logged and if yes, what does it say?
This is what it throws at me
1 Like
my guess is the file dom-test-evaluator.js is an fCC file. It’s probably broken.
1 Like
however, maybe you want to try the test in a browser that doesn’t have any extensions just in case one of your extensions is the reason for the failure
Well… it’s still not working. I’ll keep trying to change the code and keep testing with Firefox.
1 Like
facing the same error despite many tries
dhess
April 4, 2026, 2:44pm
8
Welcome to the forum @mahtabhassan159 !
Please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.
The easiest way to create a topic for help with your own solution is to click the Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge URL while still allowing you to ask any question about the challenge or your code.
Thank you.
Happy coding!
there is no point in creating a new topic in this case as the issue is likely on the fCC side, and no new information will be given in response.
dhess
April 4, 2026, 5:43pm
10
Is there a GitHub issue for this?
Edit: Nevermind; think I found it.
opened 12:40AM - 21 Jan 26 UTC
closed 08:25AM - 13 Feb 26 UTC
scope: curriculum
status: waiting triage
archived coursework
### Describe the Issue
The URL Shortener Microservice behaves correctly when te… sted locally and fully complies with the project requirements. Valid URLs are successfully shortened, and accessing /api/shorturl/<short_url> consistently redirects to the original URL. Despite this, the freeCodeCamp test interface reports that not all unit and functional tests are passing. This issue cannot be reproduced in a local environment and appears to be related to differences in the automated testing environment rather than an error in the application logic.
### Affected Page
https://github.com/yirassssindaba-coder/URL-Shortener-Microservice
### Your code
```
index.js :
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const dns = require("dns");
const app = express();
// Basic Configuration
const port = process.env.PORT || 3000;
app.use(cors());
// FCC hint: body parsing middleware untuk POST
app.use(bodyParser.urlencoded({ extended: false }));
// (Opsional, aman) support JSON body juga
app.use(express.json());
// Static + homepage (sesuai template FCC)
app.use("/public", express.static(`${process.cwd()}/public`));
app.get("/", function (req, res) {
res.sendFile(process.cwd() + "/views/index.html");
});
/**
* In-memory "database"
* (FCC tests tidak mewajibkan database sungguhan)
*/
const urlDatabase = [];
let counter = 1;
function isValidHttpUrl(input) {
try {
const u = new URL(input);
return u.protocol === "http:" || u.protocol === "https:";
} catch (e) {
return false;
}
}
/**
* POST /api/shorturl
* Body: url=<some_url>
* Response: { original_url: 'https://...', short_url: 1 }
* Invalid: { error: 'invalid url' }
*/
app.post("/api/shorturl", (req, res) => {
const originalUrl = req.body.url;
// 1) Validasi format URL harus http(s)://
if (!isValidHttpUrl(originalUrl)) {
return res.json({ error: "invalid url" });
}
// 2) Validasi host lewat DNS lookup
const parsed = new URL(originalUrl);
const hostname = parsed.hostname;
dns.lookup(hostname, (err) => {
if (err) {
return res.json({ error: "invalid url" });
}
// Kalau sudah ada, balikin yang sama (stabil untuk test)
const existing = urlDatabase.find((x) => x.original_url === originalUrl);
if (existing) {
return res.json(existing);
}
const record = { original_url: originalUrl, short_url: counter++ };
urlDatabase.push(record);
return res.json(record);
});
});
/**
* GET /api/shorturl/:short_url
* Redirect ke original_url
*/
app.get("/api/shorturl/:short_url", (req, res) => {
const shortUrl = Number(req.params.short_url);
// jika param bukan angka, tetap aman
if (!Number.isInteger(shortUrl)) {
return res.json({ error: "invalid url" });
}
const found = urlDatabase.find((item) => item.short_url === shortUrl);
if (!found) {
return res.json({ error: "invalid url" });
}
// INI yang dinilai test #3: harus redirect ke URL asli
return res.redirect(found.original_url);
});
app.listen(port, function () {
console.log(`Listening on port ${port}`);
});
```
routes/api.js:
'use strict';
const dns = require('dns');
module.exports = function (app) {
// Simpan mapping di memory (cukup untuk FCC tests)
const urlDatabase = new Map(); // short_url -> original_url
let counter = 1;
const isValidHttpUrl = (input) => {
try {
const u = new URL(input);
return u.protocol === 'http:' || u.protocol === 'https:';
} catch {
return false;
}
};
app.post('/api/shorturl', (req, res) => {
const original_url = req.body && req.body.url;
// Harus format http://... atau https://...
if (!original_url || !isValidHttpUrl(original_url)) {
return res.json({ error: 'invalid url' });
}
const hostname = new URL(original_url).hostname;
// Verifikasi hostname dengan DNS lookup (sesuai hint FCC)
dns.lookup(hostname, (err) => {
if (err) {
return res.json({ error: 'invalid url' });
}
const short_url = counter++;
urlDatabase.set(String(short_url), original_url);
return res.json({
original_url,
short_url
});
});
});
app.get('/api/shorturl/:short_url', (req, res) => {
const short_url = req.params.short_url;
const original_url = urlDatabase.get(String(short_url));
if (!original_url) {
// FCC tidak minta format khusus untuk not found,
// tapi ini aman dan tidak mengganggu tests.
return res.status(404).json({ error: 'No short URL found for given input' });
}
return res.redirect(original_url);
});
};
### Expected behavior
When a valid URL is submitted via POST /api/shorturl, the API should return a JSON response containing both the original URL and a numeric short_url identifier. When visiting GET /api/shorturl/<short_url>, the server should respond with an HTTP redirect to the original URL. This behavior works consistently when tested locally and matches the project specifications exactly. Therefore, the freeCodeCamp test labeled “Failed: 3” should pass successfully.
### Screenshots
<img width="1913" height="900" alt="Image" src="https://github.com/user-attachments/assets/555bb6ba-8ed3-4b34-b330-e1cb233491ba" />
### System
- Device: [e.g. iPhone 6, Laptop]
- OS: [e.g. iOS 14, Windows 10, Ubuntu 20.04]
- Browser: [e.g. Chrome, Safari]
- Version: [e.g. 22]
### Additional context
_No response_