const express = require("express");
const app = express();
const cors = require("cors");
app.use(cors({ optionsSuccessStatus: 200 }));
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
app.get("/api/:date?", (req, res) => {
let inputDate = req.params.date || new Date();
let parsedDate = new Date(inputDate);
if (isNaN(parsedDate.getTime())) {
return res.json({ error: "Invalid Date" });
}
res.json({
unix: parsedDate.getTime(),
utc: parsedDate.toUTCString(),
});
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log("Your app is listening on port " + listener.address().port);
});
// running tests A request to
/api/1451001600000
should return
{ unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }
// tests completed
What am I doing wrong?