My ‘Timestamp Microservice’ project is failing the test ‘Your project can handle dates that can be successfully parsed by new Date(date_string)’
I have tested the API with these methods of creating Date objects at MDN docs
let today = new Date()
let birthday = new Date('December 17, 1995 03:24:00')
let birthday = new Date('1995-12-17T03:24:00')
let birthday = new Date(1995, 11, 17) // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0)
let birthday = new Date(628021800000) // passing epoch timestamp
Please help me find what I am missing.
Here is my code
// server.js
// where your node app starts
// init project
var express = require("express");
var app = express();
// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC
var cors = require("cors");
app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204
// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (req, res) {
res.sendFile(__dirname + "/views/index.html");
});
// Here is the beginning of my own code ======================================
// function to return the required object if the argument is a valid date object
function returnRequiredObject(dateObject) {
return {
unix: new Date(dateObject).getTime(),
utc: new Date(dateObject).toUTCString(),
};
}
// function to test if the passed argument is a valid date object
function isArgumentValidDateObject(argument) {
return !isNaN(argument.getTime());
}
// Handler if no date is sent
app.get("/api/", (req, res) => {
const date = new Date();
res.send({
unix: new Date(date).getTime(),
utc: new Date(date).toUTCString(),
});
});
// Handler for all the dates submited
app.get("/api/:date", (req, res) => {
const dateArgument = req.params.date;
// if dateArgument is valid as a string
if (isArgumentValidDateObject(new Date(dateArgument))) {
return res.send(returnRequiredObject(new Date(dateArgument)));
}
// if dateArgument is valid as an Int
if (isArgumentValidDateObject(new Date(parseInt(dateArgument)))) {
return res.send(returnRequiredObject(new Date(parseInt(dateArgument))));
}
return res.send({ error: "Invalid Date" });
});
// Here is the end of my own code ============================================
// listen for requests :)
var listener = app.listen(process.env.PORT || 5005, function () {
console.log("Your app is listening on port " + listener.address().port);
});
Not sure if you fixed it but when I tested your code it passed.
As an aside, I’m not sure if timezones can affect this challenge. When I compare the dates I get in the browser using the console vs my response from repl they are different.
Thank you for the time.
The test is still not passing for me.
I also just completed the 'Request Header Parser Microservice project and it is passing all the tests. I am not sure how to know if I passed because I can’t see the blue tick