What am I doing wrong in the Timestamp?

Tell us what’s happening:
I am still struggling to pass the Timestamp project. I do not know why I am not able to pass the other 7 test requirements.

Your project link(s)

solution: https://replit.com/@artc/boilerplate-project-timestamp-2

  • I received some advice about taking a look at my routes.
  • What does this mean and where should I put this code?
  • Is this the only code I need to change in the server.js file in Replit?
// A request to /api/:date?…
app.get("/api/timestamp/:timestamp", function(req, res) {
  let timestamp = req.params.timestamp;
  if(timestamp.match(/\d{5,}/)){
    timestamp = +timestamp;
  }
  let date = new Date(timestamp);
  if(date.toUTCString() == "Invalid Date"){
    res.json({error: date.toUTCString()})
  }
  res.json({unix: dat.valueOf(), utc: date.toUTCString() });
});

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

// I was told to take a look at my routes
// A request to /api/:date?…
// where do I put this code? Do I have other syntax errors?

Thank you for any help. :slight_smile:

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:

The freeCodeCamp tests are looking for GET /api/:date? However, you currently have the following routes:

  1. GET /api/timestamp/:timestamp
  2. GET /api/timestamp/

To check if your code is working, freeCodeCamp tries to access your app and sees if it gets the right response. But it cannot do this if your routes have arbritary names. That is why it tells you how to name the routes, so that it can access them.

If you have your replit project running, click on the links under Example Usage. These links have to work in order to pass the challenge.
afbeelding

Note: the ? in /api/:date? simply means that the date parameter is optional. So, for example, both /api/2015-12-25 and /api should work.

So are you saying that I should change the code for /api/timestamp to /api/:date?

// change to app.get("/api/:date?"
app.get("/api/:date?", function(req, res) {
  let timestamp = req.params.timestamp;
  if(timestamp.match(/\d{5,}/)){
    timestamp = +timestamp;
  }
  let date = new Date(timestamp);
  if(date.toUTCString() == "Invalid Date"){
    res.json({error: date.toUTCString()})
  }
  res.json({unix: dat.valueOf(), utc: date.toUTCString() });
});

// change to app.get("/api/:date?"
app.get("/api/:date?", (req, res) => {
  let date = new Date();
  res.json({unix: date.valueOf(), utc: date.toUTCString() });
})
  • Should I also change anything with the code “timestamp” to “date”?
  • Did I write the codes for UNIX & UTC correctly?
  • What changes in my code should I make?

Now you have two routes that are identical (/api/:date?), which doesn’t work. The easiest way is to change:

  1. /api/timestamp/:timestamp to /api/:date
  2. /api/timestamp/ to /api

You will have to change how you access the request parameter:
let timestamp = req.params.timestamp will no longer work, so you have to change that to let timestamp = req.params.date

The rest of the code seems fine, except for one typo:

res.json({unix: dat.valueOf(), utc: date.toUTCString() });
                ^^^

Thank you for the syntax correction and tip. I am able to pass most of the test.

I was able to get the code to run on Replit.com and it allowed me to click on the url links
and I can see the time and date.

Now, I am dealing with the last two challenges, which says I need to
return current time in a JSON object with a unix & utc keys.

freeCodeCamp, last two test needed:

  1. An empty date parameter should return the current time in a JSON object with a unix key
  2. An empty date parameter should return the current time in a JSON object with a utc key
// changed code for ' app.get("/api/:date? '
app.get("/api/:date?", function(req, res) {
  let timestamp = req.params.date; // changed code 'req.params.date'
  if(timestamp.match(/\d{5,}/)){
    timestamp = +timestamp;
  }
  let date = new Date(timestamp);
  if(date.toUTCString() == "Invalid Date"){
    res.json({error: date.toUTCString()})
  }
  res.json({unix: date.valueOf(), utc: date.toUTCString() });
});

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

Which area in my code am I missing the unix key and the utc key for JSON object?

You have to change /api/:date? to /api/:date. By adding the question mark, it makes the date parameter optional.

So if you go to /api it still matches with /api/:date? and run that code instead of the code inside the /api route.

Ok, I finally got it. Thank you so much! I see that I needed to deal with the app.get and the req.params.date parts.

Thank you so so much!

code solution

app.get("/api/:date", function(req, res) {
  let timestamp = req.params.date; // changed code 'req.params.date'
  if(timestamp.match(/\d{5,}/)){
    timestamp = +timestamp;
  }
  let date = new Date(timestamp);
  if(date.toUTCString() == "Invalid Date"){
    res.json({error: date.toUTCString()})
  }
  res.json({unix: date.valueOf(), utc: date.toUTCString() });
});

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

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