Back End Development and APIs Projects - Timestamp Microservice

Tell us what’s happening:

my code:

app.get("/api/:date?", (req, res) => {
  const dateParam = req.params.date;
  let date;


if (!dateParam) {
    // create DateConstructor with the currant time
    date = new Date();
}
else if () {
    // the resat of the handler logic
}

  const unix = date.getTime();
  const utc = date.toUTCString();
  res.json({
    unix: unix,
    utc: utc
  })
  return;
});

tests results

  • Passed: A request to /api/:date? with a valid date should return a JSON object with a unix key that is a Unix timestamp of the input date in milliseconds (as type Number)

  • Passed: A request to /api/:date? with a valid date should return a JSON object with a utc key that is a string of the input date in the format: Thu, 01 Jan 1970 00:00:00 GMT

  • Passed: A request to /api/1451001600000 should return { unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }

  • Passed: Your project can handle dates that can be successfully parsed by new Date(date_string)

  • Passed: If the input date string is invalid, the API returns an object having the structure { error : "Invalid Date" }

  • Failed: An empty date parameter should return the current time in a JSON object with a unix key

  • Failed: An empty date parameter should return the current time in a JSON object with a utc key

the issue

is that when i run the server and test the response of the https://host-url/api i get the right json output

for example
{"unix":1709055160997,"utc":"Tue, 27 Feb 2024 17:32:40 GMT"}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0

Challenge Information:

Back End Development and APIs Projects - Timestamp Microservice

I don’t see how you can pass those tests with the code you posted. It should crash and there are missing cases.

  1. The if else condition can’t be empty and it doesn’t make sense that the code block is empty as well.

  2. Even if you turn the if else into an else and move the code inside it you will still crash from calling date.getTime() when dateParam is truthy. You only use the Date constructor if dateParam is falsy so date is undefined otherwise.

How are we supposed to test your code if you do not post all of it?

Anyway, the code you posted is passing all the tests for me (using Gitpod). How are you running the code?

I also use gitpod.

https://3000-freecodecam-boilerplate-fgriwptv9iy.ws-eu108.gitpod.io

these is the link of my workspace api.

It’s works fine but not passing all the tastes.

Your code is passing for me.

Make sure your system clock is correct (sync the clock with a time server. The OS you are using should have an option for it).

Thanks lasjorg.

When you mentioned the system clock, it triggered a moment of realization about the root cause of the issue.

What was the issue?

I guess that the tests logic is checking the values of the response too.
I mean i am using the browser in my pc and using gitpod for hosting the api server and the time on my pc and gitpod server or workspace does not match.

How did i solved it?

  • I have downloaded my workspace and started it locally.
  • Then i exposed the port that my server uses, using localtunnel package.
    npx localtunnel --port PORT
  • Used the url that localtunnel gave me for tasts and passed all tests.

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