Tell us what’s happening:
My code for the timestamp microservice works fine for all the challenges except one. However when I use a browser to issue that get request the response looks exactly correct so I’m mystified as to what the challenge validation does not like about my response. Here is the one challenge requirement is not passing:
"A request to /api/1451001600000
should return { unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }
"
Here is the response I get in a browser when use that URL with my endpoint:
{“unix”:“1451001600000”,“utc”:“Fri, 25 Dec 2015 00:00:00 GMT”}
The full url used to get this response from my server is:
https://boilerplate-project-timestamp.cavasian.repl.co/api/1451001600000
Your project link(s)
solution: https://replit.com/@cavasian/boilerplate-project-timestamp
Here is my code:
// Date microservice starts here
app.get("/api/:date?",(req,res) => {
const dateStr = req.params.date;
/* If no date parameter is present then respond with current date in both formats */
if (!dateStr){
let date = new Date();
return res.json({unix: date.getTime(), utc: date.toUTCString()});
}
if (isNaN(dateStr)){
/* Date parameter IS NOT a number */
let dateObj = new Date(dateStr);
if (dateObj.toString() === 'Invalid Date'){
return res.json({error: "Invalid Date"});
}else {
return res.json({unix: dateObj.getTime(), utc: dateObj.toUTCString()});
}
}
/* Date parameter IS a number*/
let dateObj = new Date(parseInt(dateStr));
if (dateObj.toUTCString() === 'Invalid Date'){
return res.json({error: "Invalid Date"});
} else {
return res.json({unix: dateStr, utc: dateObj.toUTCString()});
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36
Challenge: Timestamp Microservice
Link to the challenge: