I haven’t passed test #5 from the Timestamp Microservice project of the Back End Development and APIs certification: "Your project can handle dates that can be successfully parsed by new Date(date_string)".
I’ve tried deploying it to Heroku and pasting that app as the solution link as well as the app I created on Replit.
This my code so far:
// 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');
});
// your first API endpoint...
app.get("/api/hello", function (req, res) {
res.json({greeting: 'hello API'});
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
let resObject = {}
app.get('/api/:input', (req, res) => {
let input = req.params.input
if(input.includes('-')){
/* Date String */
resObject['unix'] = new Date(input).getTime()
resObject['utc'] = new Date(input).toUTCString()
}else{
/* Timestamp */
input = parseInt(input)
resObject['unix'] = new Date(input).getTime()
resObject['utc'] = new Date(input).toUTCString()
}
if(!resObject['utc'] || !resObject['unix']){
res.json({error: 'Invalid Date'})
}
res.json(resObject)
})
app.get('/api', (req, res) => {
resObject['unix'] = new Date().getTime()
resObject['utc'] = new Date().toUTCString()
res.json(resObject)
})
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36
Challenge: Timestamp Microservice
Link to the challenge:
