Back End Development and APIs Projects - Timestamp Microservice

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?

You are returning {"error":"Invalid Date"}

Here is a hint:

isNaN(new Date(1451001600000).getTime())
// false

isNaN(new Date("1451001600000").getTime())
// true

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.