Timestamp microservice project issue

I’m totally confused on how to build timestamp microservice project.
I’ll be glad if someone can help me fix it.
I only setup index.html and server.js in replit
Do I need to add additional code or what’s wrong with because it’s only pass one test out of eight.
Replit code link
Timestamp link

If you run the fCC tests against your project with the browser console open, you will see many Cannot GET errors. If you check the specifications:

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

Your route

app.get("/api/timestamp/:date", (req, res) => {

is not listening at the correct endpoint.

Okay, am gonna try to correct that now

I unable to pass the test again after I rewrite the code.

app.get("/api/hello", function (req, res) {
  res.json({greeting: 'hello API'});
});

app.get("/api/timestamp", (req, res) => {

  res.json({ unix: Date.now(), utc: Date() });

});
app.get("/api/timestamp/:date_string?", (req, res) => {

  let dateString = req.params.date_string;

  //A 4 digit number is a valid ISO-8601 for the beginning of that year

  //5 digits or more must be a unix time, until we reach a year 10,000 problem

  if (/\d{5,}/.test(dateString)) {

    const dateInt = parseInt(dateString);

    //Date regards numbers as unix timestamps, strings are processed differently

    res.json({ unix: dateInt, utc: new Date(dateInt).toUTCString() });

  } else {

    let dateObject = new Date(dateString);

    if (dateObject.toString() === "Invalid Date") {

      res.json({ error: "Invalid Date" });

    } else {

      res.json({ unix: dateObject.valueOf(), utc: dateObject.toUTCString() });

    }

  }

});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});

I guess, I need clear explanation to be able to pass this test. And is there any setup that I need to fix apart from server.js to pass the test?

Specified API endpoint:

/api/:date?

Your API endpoint:

/api/timestamp/:date?

These are not the same.

1 Like

I later resolved the issue yesterday. Thank you so much

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