I wrote an implementation of the Timestamp Microservice for the APIs and Microservices projects, but it’s not passing any of the tests (other than the one confirming I’ve used my own code URL). I’m not sure why it’s not passing any of the tests since I’ve tested with my own inputs and am getting the right results.
Would using Moment.js for time/date manipulation cause the tests to fail? I didn’t see anything indicating we had to write the application using default Date methods.
I wrote my code on the server.js
file provided in the project GitHub repository.
Here is a link to my live project on Glitch: http://timestamp-emc-fcc.glitch.me/api/timestamp/
Here is my code so far:
/**gets current time from the route /api/timestamp*/
app.get(
"/api/timestamp",
(req, res, next) => {
req.utc = new Date().toString();
req.unix = new Date().getTime();
next();
},
(req, res) => {
res.json({
unix: req.unix,
utc: moment(req.utc).format("ddd, D MMM YYYY HH:mm:ss") + " GMT"
});
}
);
/**gets time based on the route provided, gets both unix and utc inputs*/
app.get(
"/api/timestamp/:date",
(req, res, next) => {
const myDate = req.params.date;
if (moment(myDate).isValid()) {
req.utc = myDate.toString();
req.unix = moment(myDate).valueOf();
next();
} else if (moment.unix(myDate).isValid()) {
req.utc = moment.unix(myDate / 1000).toString();
req.unix = parseInt(myDate);
next();
} else {
res.json({ error: "Invalid Date" });
}
},
(req, res) => {
res.json({
unix: req.unix,
utc: moment(req.utc).format("ddd, D MMM YYYY HH:mm:ss") + " GMT"
});
}
);
I plan to rewrite the application without using Moment.js, but I’d also like to find out where I went wrong with this implementation. Thank you for your ideas and input in advance!
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36
.
Challenge: Timestamp Microservice
Link to the challenge: