// index.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');
});
const isInvalidDate = (date) => isNaN(date.getTime());
// your API endpoint...
app.get("/api/:date", function (req, res) {
let date = new Date(req.params.date);
if(isInvalidDate(date)){
date = new Date(+req.params.date);
}
if (isInvalidDate(date)) {
res.json({ error: "Invalid Date" });
return;
}
res.json({
unix: date.getTime(),
utc: date.toUTCString()
});
});
// Handle empty date parameter
app.get("/api", (req, res) => {
const currentDate = new Date();
res.json({
unix: currentDate.getTime(),
utc: currentDate.toUTCString()
});
});
// Listen on port set in environment variable or default to 3000
var listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
I’ve been working on a project in Express.js called “Timestamp Microservice” as part of my learning journey. While my code seems correct, I’m encountering an issue where the output returns the wrong time, despite the date being accurate. I’ve double-checked my implementation, including parsing the date correctly, but the time displayed doesn’t match what I expect. I’ve tried debugging by logging intermediate steps, but I’m still unable to pinpoint the source of the discrepancy. Any insights or suggestions on how to troubleshoot and resolve this issue would be greatly appreciated!