Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:

hi!
every step is correct except for step 3, i have tried different repositories, tried gtp, tested many solutions but nothing seems to work. can anyone tell me what im doing wrong?

require(‘dotenv’).config();

const express = require(‘express’)

const app = express()

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

app.use(express.urlencoded({ extended: false }));

app.use(express.json());

const cors = require(‘cors’);

app.use(cors({optionsSuccessStatus: 200}));

app.use(express.static(‘public’));

const dns = require(‘node:dns’);

const urls = [];

app.get(‘/’, (req, res) => {

res.sendFile(__dirname + ‘/views/index.html’);

})

app.post(‘/api/shorturl’, (req, res) => {

const originalUrl = req.body.url;

let hostname;

try {

hostname = new URL(originalUrl).hostname;

} catch {

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

}

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

if (err) return res.json({ error: ‘invalid url’ });

urls.push(originalUrl);

const shortUrl = urls.length;

return res.json({ original_url: originalUrl, short_url: shortUrl });

});

});

app.get(‘/api/shorturl/:short_url’, (req, res) => {

const short_url = parseInt(req.params.short_url) - 1;

const originalUrl = urls[short_url];

if (originalUrl) {

res.redirect(originalUrl);

} else {

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

}

})

app.listen(port, () => {

console.log(`Example app listening on port ${port}`)

})


###Your project link(s)

Your browser information:

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

Challenge Information:

Back End Development and APIs Projects - URL Shortener Microservice

Hi, still nothing. Anyone?

please provide a repository that we can run and a link to the challenge

oh sorry. here:

solution: http://localhost:3000

do you also have the link to the challenge page on freeCodeCamp?

this one:

do you maybe also get a cors error in the browser console?

image

if not, what other errors (red messages) do you see?

i get no errors, either in the browser console or in the terminal :melting_face:

that’s impossible, if the test is failing it will output the error in the console
in which page are you looking at the console? there isn’t a terminal in the challenge page

oh sorry, i was looking at the localhost console

when i run the tests on the fcc challenge page, the only red error i see is from the runner itself:
dom-test-evaluator.js:2 Error: redirected is not implemented yet

does that not say that the redirecting does not work? it’s what test 3 is testing

redirect works correctly — curl -I http://localhost:3000/api/shorturl/3 returns HTTP/1.1 302 Found with Location: ``https://freecodecamp.org.

to make sure persistence wasn’t the issue, i also switched the implementation to use MongoDB (so the data survives restarts and different test runs), but test 3 still fails the same way on the challenge page

when running the tests, the only error shown in the browser console is:
dom-test-evaluator.js: Error: redirected is not implemented yet

it looks like the evaluator crashes while trying to access fetch’s response.redirected property, before it can actually assert the redirect behavior

if you think it’s an issue with the tests, please open an issue on github about it

1 Like