Timestamp project

I do not understand what the reason why is this is not passing all of the tests:

// index.js
// where your node app starts

// init project
var express = require('express');
var app = express();
let bodyParser=require('body-parser');

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

//it is to read different kind of information
app.use(bodyParser.urlencoded({extended: false}));

// 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",function(req,res){
  let utcCurrentTime = new Date().toUTCString(); 
  let unixCurrentTime = Date.now();
  res.json({unix:utcCurrentTime, utc: unixCurrentTime});
  //res.json({unix:utcCurrentTime})
});

//fourth endpoint


app.get("/api/:date",function(req,res){

  let date = parseInt(req.params.date);

   if((Date.parse)){//if true, continues with the transformation
      let seconds = new Date(date);
  let utcTimestamp = seconds.toUTCString();
  res.json({unix:date, utc: utcTimestamp})
   }else{
     res.json({error:"Invalid Date"})
  }
  
})

        
  1. Your unix and utc values are not correct.

Request URL: https://boilerplate-project-timestamp.aaronargotte.repl.co/api/2016-12-25

Response: {"unix":2016,"utc":"Thu, 01 Jan 1970 00:00:02 GMT"}

Request URL: https://boilerplate-project-timestamp.aaronargotte.repl.co/api/05%20October%202011,%20GMT

Response: {"unix":5,"utc":"Thu, 01 Jan 1970 00:00:00 GMT"}

  1. Your error response is not correct.

Request URL: https://boilerplate-project-timestamp.aaronargotte.repl.co/api/this-is-not-a-date

Response: {"unix":null,"utc":"Invalid Date"}

If the input date string is invalid, the API returns an object having the structure { error : "Invalid Date" }


Use the browser dev tools to look at the request/response when you submit the code.

I would also suggest some Googling around the date formats you are asked to use.

1 Like

Thanks! I was testing this wrongly

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