Hi, I am working on Timestamp Microservice and I can’t pass the two last tests:
-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
I can’t understand where my mistake is in my code and what am I not seeing. I would appreciate if somebody could take a look at it and let me know if they see a problem. Much thanks!
// 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({optionSuccessStatus: 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');
});
// your first API endpoint...
app.get("/api/hello", function (req, res) {
res.json({greeting: 'hello API'});
});
app.get('/api/timestamp/:date_string', function(req, res){
let time;
let regex = RegExp(/^\d*$/g);
//this is the piece that is supposed to handle empty date parameters
if(req.params.date_string === null){
time = new Date(Date.now())
}else if(regex.test(req.params.date_string)){
time = new Date(parseInt(req.params.date_string, 10))
}
else{
time = new Date(req.params.date_string)
}
console.log(time)
if(Number.isNaN(time.getTime())){
res.json({'error': "Invalid Date" })
}
else{res.json({"unix": time.getTime(), "utc": time.toUTCString()})}
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});