Timestamp Microservice - does't pass the tests

hi everyone, this is the code I wrote in order to pass the challenge:

// init project
var express = require('express');
var app = express();

var newDate, unixDate, utcDate;
var objDate = {};

const regexISO = new RegExp('([0-9]+(-[0-9]+)+)');
const regexUNIX = new RegExp('([0-9]+)');

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

  objDate = {};
  
  console.log(req.params)
  const {date} = req.params;

  //if it is ISO ---------------
  if(regexISO.test(date)){
    myDate = date.split("-");
    newDate = new Date( myDate[0], myDate[1]-1, myDate[2]);
    unixDate = newDate.getTime();
    
    utcDate = new Date(date).toUTCString();
    
    objDate['unix'] = unixDate;
    objDate['utc'] =  utcDate;

   // console.log(newDate)
  //if it is UTC ---------------
  }else if(date.includes('GMT')){
    unixDate = new Date(date).getTime()
    
    objDate['utc'] = date;
    objDate['unix'] = unixDate;

  //if it is UNIX ------------
  }else if(regexUNIX.test(date)){
    utcDate = parseInt(date);
    objDate['unix'] = utcDate;
    
    utcDate = new Date(utcDate)
    objDate['utc'] =  utcDate.toUTCString();

  //if is Inavalid ------------------
  }else{
    objDate = { error : "Invalid Date" }
  }
  //date = new Date(date);
  console.log(objDate)
  res.json(objDate);
}) ;


app.get('/api', (req, res) => {
  objDate['utc'] = new Date().toUTCString();
  objDate['unix'] = Date.now();
  res.json(objDate);
})

And this is how it looks:
https://boilerplate-project-timestamp.davitboo.repl.co/

Also here all the files:

everytime I try to pass the challenges I just get the first check, the one that says:
“Passed:You should provide your own project, not the example URL.”

So what I am missing here?
Thank you in advance

  1. You are getting CORS blocked. Move the cors middleware up right below var app = express();
var app = express();
var cors = require('cors');
app.use(cors({optionsSuccessStatus: 200}));  // some legacy browsers choke on 204
  1. You are not using toUTCString in the GMT condition for the UTC time.
1 Like

Thank you so much! Now the checks work at least!

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