Timestamp Microservice

This has me really confused. I’m not sure what to do, I am guessing projects have no directions and I just need to look at all the test that need to be passed? In that case I am trying to solve this one but don’t know what to put inside my json:

A request to /api/:date? with a valid date should return a JSON object with a unix key that is a Unix timestamp of the input date in milliseconds (as type Number)

// 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

// 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'});

app.get("/api/:date?", (req, res) => {

// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);

You have to use the parameter and construct the two types of dates that are asked for in the response. You also have to handle when the parameter is missing or contains a value that is invalid when constructing a date.

The challenge is really more about JS dates than anything.

So far, you haven’t even used the parameter and I see no attempt at constructing the dates that are asked for.

app.get("/api/:date?", (req, res) => {
  const dateParam = req.params.date; 
  const dateObject = new Date(dateParam); 

  if (isNaN(dateObject.getTime())) {
    return res.json({ error : "Invalid Date" }); 

  const unixTimeStamp = dateObject.getTime(); 
  const utcString = dateObject.toUTCString(); 

  res.json({ unix: unixTimeStamp, utc: utcString }); 

Struggling with Tests 4, 7, and 8. Can you help me understand what I need to do to pass those?

Figured it out, I need to really make a habit of breaking things down with notes more often. Struggled with realizing I had to use regex and ParseInt. Rewrote this like 5 times to make sure it’s really understood:

app.get("/api/:date?", (req, res) => {
  // Extract date parameter from request 
  const dateParam = req.params.date; 
  // Initialize variable to hold date object
   let dateObject; 
  // if no date parameter is provided, use current date
  if (!dateParam) {
    dateObject = new Date(); 
  // if parameter is in unix timestamp format(5 or more digits), then convert it to a date object
  else if (/\d{5,}/.test(dateParam)) {
    dateObject = new Date(parseInt(dateParam)); 
  // if date parameter in a string, then convert it to  a date object 
  else {
    dateObject = new Date(dateParam); 
  // if date object is invalid, return a error response ({ error: "Invalid Date" })
  if (isNaN(dateObject)) {
    return res.json({ error: "Invalid Date" }); 
  // if date is valid, return unix timestamp and utc string
  else {
    return res.json({unix: dateObject.getTime(), utc: dateObject.toUTCString()}); 
1 Like

You do not have to use a regex.

As long as the API you are using (e.g. the Date API) handles incorrect input and gives back an error message (Invalid Date) you can always pass the API invalid inputs and check the return value. Just like how you can check if a string is a “number” by looking at the return value from the method used to do the conversion (NaN).