The tests are running for the POST API - /api/shorturl/new. But for the GET API - api/shorturl/<short_url>, when I try it in the browser, it gets redirected all right. But the tests are not running. When I look into the logs, I see a param “undefined” as the short_url params (the :id param in my code). Now the example given by freecodecamp, I referred to it regarding this case and it showed response with json {"error":"Wrong format"}
, along with status code 304. I am also returning the same. The other redirects, like /api/shorturl/3 redirects are also working fine in the browser. With 302 status. Yet, the tests don’t run.
Your code so far
Here is the code for the GET route -
app.get("/api/shorturl/:id", async (req, res) => {
try {
console.log(req.params, req.url);
const id = parseInt(req.params.id);
if (isNaN(id)) throw new Error("Wrong format");
const queryGetShortUrl = `SELECT * FROM url.short_urls WHERE _id = ?;`;
let results = await query(queryGetShortUrl, [id]);
if (results.length === 0) throw new Error("not found");
const { url } = results[0];
res.redirect(url);
} catch (err) {
if (err.message === "not found")
res.json({ error: "No short URL found for the given input" });
else res.status(304).json({ error: "Wrong format" });
}
});
You can also look into the git repo for this. In this, you can look into server.js.
Please help me. I am stuck.
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36
.
Challenge: URL Shortener Microservice
Link to the challenge: