one of my route is never visited if i run the tests:
app.get("/api/shorturl/:id", async (req, res) => {
let id = req.params.id
console.log(`42: id = ${id}`);
let url = await urlModel.findOne({ short_url: id })
console.log(`44: id = ${id} = url.original_url = ${url.original_url}`);
//res.redirect(url.original_url)
res.json(url); // JSON response with original_url and short_url
})
That is because there is not test that makes a GET request to a/api/shorturl/:id route. Look at the instructions for the type of request that is expected.
i have deleted most in my index.js in last minutes (if route not woks i not need anything else. therfore i delete everything first and try find the route with sended data first)
but created a backup to index backup.js
That means something must have interfered with the proper execution (in the index I’ve been using till today, around 7pm Europe time).
the index that i have used the last 2 days with the same rout gets my
a empty object with same source (at this route).
but in my last index there was/is much stuff into . about 400 lines of code. some side-effect maybe. or i dont know. i need find out or need to programing it completely fresh (maybe not bad idea)
index old (last 2 day or so get my no data) near line 150:
app.get("/api/shorturl/:id", async (req, res) => {
let id = req.params.id;
index new (today evening) near line 20 that gives me data:
app.post("/api/shorturl/id", async (req, res) => {
let id = req.body.id;
that was the moment i created many routes to try to get some data from somewhere
Btw there was strange behavior today. website was reloading very often. and was not easy to edit (this maybe happens using large files). BTW i closed some clipboard apps at my Desktop like AutoKey-App. Seems helps a bit.
I would start over. You have a ton of code that is not doing anything useful. Have you gone through all the backend challenges for Node and Express. You might benefit from going though them again as it seems from some of the code you have written that you may not quite understand how to create the correct routes.
i got a get result, that looks like it was sendet to my scritpt to my script to itself:
npm run start
> shorturl@0.0.3 start
> node index.js
Listening on port 3000
....
/‾‾‾ req.params object‾‾‾‾‾‾‾‾‾ 35
{ id: 'dummy' }
\___ req.params _________ 35
not falid .... EndOf 79
not valid = .... 26
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:393:5)
at ServerResponse.setHeader (node:_http_outgoing:644:11)
at ServerResponse.header (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/response.js:794:10)
at ServerResponse.send (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/response.js:174:12)
at ServerResponse.json (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/response.js:278:15)
at /home/runner/boilerplate-project-urlshortener/index.js:31:9
at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/route.js:144:13)
at Route.dispatch (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/layer.js:95:5)
Are you talking about a route the tests request or one that you manually tested yourself. If it is one you tested yourself, what is the route, is it a GET or POST route, and how did you attempt to request it?
i not test myself. i mean the tests from https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/url-shortener-microservice
I didn’t know that data from myself might come to my script from another source (apart from my index.html or so).