What am I doing wrong in the Timestamp project?

Tell us what’s happening:

I do not understand why I am not able to pass the freeCodeCamp test for Timestamp.

I am using Replit starter project.
I did the coding in the server.js file underneath the commented code, // your first API endpoint…

I am able to pass the first test, providing my own project on Replit. But I cannot seem to pass the other 7 tests below.

  • Am I missing something?
  • Do I have to include a Secret Environment Variable file?
  • Is the server.js file the only place I do coding?
  • Do I have to do something in the package-lock.json file or the package.json file?
  • Is my code lacking something or have syntax errors?

I appreciate any help. Thanks :slight_smile:

Your code so far

app.get("/api/timestamp/:timestamp", function (request, response) {
  let timestamp = request.params.timestamp;
  if(timestamp.match(/\d{5,}/)){
  timestamp = +timestamp;
  }
    
  let date = new Date(timestamp);
  if(date.toUTCString() == "Invalid Date"){
    response.json({error: date.toUTCString()})
  }

  response.json({unix: valueOf() , utc: date.toUTCString() });  
});

app.get("/api/timestamp/",(req, res) => {
  let date = new Date();
  response.json({unix: valueOf() , utc: date.toUTCString() }); 
})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Timestamp Microservice

Link to the challenge:

Hello there,

Take a closer look at your routes:

A request to /api/:date?

Hope this helps

Ok, I changed to :date?

// your first API endpoint…
app.get("/api/timestamp/:date?", function (request, response)
// First code line

But I am still not able to get the API data when I click on the two project url links.

  • [project url]/api/2015-12-25]
  • [project url]/api/1451001600000]

I am still working this.
Thanks for responding to me first.

Take a close look :wink:

Hello,
What exactly did you mean by /api/:date?

I changed some of the code timestamp to :date?
Is this what you mean?
My Code

/* Add /api/:date? */

app.get("/api/timestamp/:date?", function (request, response) {
let date = request.params.date;
if(date.match(/\d{5,}/)){
date = +date;
}

let date = new Date(date);
if(date.toUTCString() == “Invalid Date”){
response.json({error: date.toUTCString()})
}

response.json({unix: valueOf() , utc: date.toUTCString() });
});

app.get("/api/:date?",(req, res) => {
let date = new Date();
response.json({unix: valueOf() , utc: date.toUTCString() });
})

This route is not used by the tests.

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