I am almost done the Timestamp project. I have ticked all the criteria boxes. The only thing missing are the last two where the first one is " An empty date parameter should return the current time in a JSON object with a unix key" and the second one similar but focused on “utc”. When i run the project locally or on repl and dont put the date or anything it still comes back wrong. i am confused. Here is my code:
date_params.getTime() is a number and is the only one that returns time in milliseconds. I don’t know if you were leading to something else as date_params itself takes in “new Date(req.params.date)” so that is still an integer.
I tried handling the “blank” input of date like you did. It still does not mark the answer as right even though now we are directly addressing the lack of dates for input. Very strange. can’t understand it. It still works properly on my machine and yours now. Thanks for suggesting this though! it made me realise the error may not necessarily be on our side.
I’ve made a tiny change to your code (exact same structure is left), we still don’t pass, but this is how it looks:
app.get("/api",function(req, res) {
//take what the user sent first
const current = Date.now();
const response = {
"unix": parseInt(current,10),
"utc": (new Date()).toUTCString()
}
console.log(response)
res.json(response)
}
)
app.get("/api/:date", function(req, res) {
//take what the user sent first
const date = req.params.date;
let date_params = new Date(date);
if(date_params.toString() === "Invalid Date"){
res.json({ error: "Invalid Date"});
}
else if(date_params.toString() !== "Invalid Date"){
res.json({ "unix": Number(date_params.getTime()), "utc": date_params.toUTCString()});
}
});
So, in my forked boilerplate, that passes the first several tests (same than in yours, basically.)
However, I remind you that the exercise specifically uses /api/:date? and you would ideally merge the first and second get into one. But I leave the fun for you.
I believe the question mark indicates express to catch the request if the parameter is not there.