Tell us what’s happening:
I’ve created an API endpoint to handle an empty date parameter, while testing it works fine the same way the FCC model works. But when I submit, it keeps returning this:
It should handle an empty date parameter, and return the current time in unix format
It should handle an empty date parameter, and return the current time in UTC format
Your code so far
app.get("/api/timestamp/", (req, res) => {
res.json({unix: new Date().valueOf(), utc: new Date().toUTCString()});
});
app.get("/api/timestamp/:date_string", (req, res) => {
let dateString = req.params.date_string;
console.log(dateString);
//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)) {
let dateInt = parseInt(dateString);
//Date regards numbers as unix timestamps, strings are processed differently
res.json({ unix: dateString, utc: new Date(dateInt).toUTCString() });
}
let dateObj = new Date(dateString);
if (dateObj.toString() == "Invalid Date") {
res.json({ error: "Invalid Date" });
} else {
res.json({ unix: dateObj.valueOf(), utc: dateObj.toUTCString() });
}
});
links:
live app: https://destiny-nutria.glitch.me
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
.
Challenge: APIs and Microservices Projects - Timestamp Microservice
Link to the challenge: