Solution suggestion for APIs and Microservices Projects - Timestamp Microservice

Here is an endpoint that handles “/api/timestamp/” and “/api/timestamp/:date?” at the same time.

Solution
app.get("/api/timestamp/:date?", (req, res)=>{
  
  //Date parameter is in url
  if (req.params.date){
    //If date is all number, pass it as number to Date(), else pass it as string
    if (/^\d+$/.test(req.params.date)){
      date = new Date(+req.params.date)
    } else {
      date = new Date(req.params.date)
    }
    //Response depends on if the date parameter is a valid date
    if (date == "Invalid Date"){
      res.json({
        "error": "Invalid Date"
      })
    } else {
      res.json({
        "unix": date.getTime(),
        "utc": date.toUTCString(),
      })
    }
  //Date parameter in url is empty
  } else {
    res.json({
      "unix": new Date().getTime(),
      "utc": new Date().toUTCString()
    })
  }
})

Challenge: Timestamp Microservice

Link to the challenge:

p.s. this is the first time i’m posting a solution suggestion, please let me know if there’s a format that I should be following. Thanks.