Back End Development and APIs Projects

I am working on the timestamp microservice, and my code seems to work, but the test is saying everything isn’t working.

Aside from the get(“/api/:date?”) function, what else needs to be done for the test to pass?

app.get("/api/:date?", (req, res) => {
  if(!req.params.date){
    
    let date = new Date();
    let ut = date.toUTCString();
    let un = date.getTime()
    res.json({unix: un, utc: ut });

  } else {
    let date = new Date(req.params.date);
    if(date.toUTCString() === "Invalid Date") date = new Date(+req.params.date);
    let ut = date.toUTCString();
    let un = date.getTime()
    res.json({unix: un, utc: ut });
  }
});

I tested it on localhost and It seems to work just fine.

if I navigate to localhost:3000/api/ I get

{
“unix”: 1734198747236,
“utc”: “Sat, 14 Dec 2024 17:52:27 GMT”
}

if navigate to /api/Sat,%2014%20Dec%202024%2017:50:40%20GMT or /api/1734198640000, I can see a json file that says
{“unix”:1734198640000,“utc”:“Sat, 14 Dec 2024 17:50:40 GMT”}

I tried to put my project on gitpod and open a port to test my project, and free code camp is saying none of it works when I tested myself and it does.

So is there something else I have to do? Perhaps I made an error or I am misunderstanding the project.

what link are you submitting?

the port is closed right now, but the link was https://3000-jsjm25-timestampmicrose-mq9ndycw3oa.ws-us117.gitpod.io/ and I also tried to open a port locally on my pc and the link was a usw2.devtunnels.ms/ link from the port I opened in vs code.
I edited the html file to add a button that generates a time stamp link.

I also have a part of my script that logs requests.

app.use((req, res, next) => {
  let s = new Date();
  console.log(`${req.method}, ${req.path} - ${req.ip}  at ${s.valueOf()}`);
  next();
});

so I can see in my terminal that freecodecamp is making requests and when I make similar requests the json file I receive seems to be correct so I’m not entirely sure why its saying my code fails the test.

did you use the given template link to start working on this?

I found the solution just now from another post on the forum.

I realized the problem was because I didn’t have cors added into my index.js script.

Not sure if you made other changes but in the code you posted you are not returning the correct object. You are asked to return { error : "Invalid Date" } but you are returning {"unix":null,"utc":"Invalid Date"}

You can’t use the same logic you have for both handling when the param is a number and an invalid time string. You would need to check it again after the type convention and date constructor code and if it still an invalid date return the expected error object.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.