Timestamp Microservice: 3. If the date_string is empty

Timestamp Microservice: 3. If the date_string is empty
0

Hi,
My project almost done but I stuck on a bagatel point:

If the date_string is empty I get an error and I not able to handle it…
My glitch is here
ISO format works well.
Timestamp format works well.
Invalid format works well. (I used this instruction of number 5)
Any empty query is not work at all: I get the following error: Cannot GET /api/timestamp/

I cannot to handle this error inside the middleware function because this function is not executed if I ask empty query… so how can I handle it?
Maybe with try { } catch {} ???

My code is here:

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

// init project
const express = require("express");
const app = express();

// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC 
const cors = require("cors");
app.use(cors({
  optionSuccessStatus: 200
}));  // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
  response.sendFile(`${__dirname}/views/index.html`);
});

// create result object which is what will be returned by our API:
const createResultObject = () => {
  return {
    "unix": null, 
    "utc": null
  };
};

// 1. Set the API endpoint:
const endpoint = "/api/timestamp/";


app.get(`${endpoint}:date_string`, (request, response) => {
  //console.log(request.url);
  //console.log(request.params.date_string);
  const result = createResultObject();

// 2. Check for valid date_string:
  // set timestamp to a variable: sended as a string, so we have to change the type to Number
  let timestamp = + request.params.date_string;
  
  // check if the timestamp is invalid:
  if (!(timestamp)) {
    timestamp = Date.parse(request.params.date_string); // set the timestamp from ISO format
  }
  
  // 5. check if the timestamp still invalid (neighter number nor ISO format...)
  if (!(timestamp)) {
    response.send({
      error: "Invalid Date"
    });
    return;
  }
  
  // 4. From here we have a valid timestamp:
  // create a new Date object using  the above timestamp:
  const date = new Date(timestamp);
    
  // set "unix" value in our result object:
  result.unix = timestamp;
    
  // build "utc" value in result object:
  result.utc = date.toUTCString();

  // send this response back to the browser:
  response.send(result);
});

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log(`Your app is listening on port ${listener.address().port}`);
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.116.

Link to the challenge:
https://learn.freecodecamp.org/apis-and-microservices/apis-and-microservices-projects/timestamp-microservice/

After a few tries I found this solution but I am not sure this is the right way… :frowning:
I created another app.get() method without any variable…
I also correct a mistake: without the second condition the code make unwanted result:

// check if the timestamp is invalid:
  if (!(timestamp) && timestamp !== 0) {...}

I would be grateful if anyone take a look to my code before I post it…

My code here:

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

// init project
const express = require("express");
const app = express();

// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC 
const cors = require("cors");
app.use(cors({
  optionSuccessStatus: 200
}));  // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
  response.sendFile(`${__dirname}/views/index.html`);
});

// create result object which is what will be returned by our API:
const createResultObject = () => {
  return {
    "unix": null, 
    "utc": null
  };
};

// 1. Set the API endpoint:
const endpoint = "/api/timestamp/";

const sendResult = (timestamp, response) => {
  
  const result = createResultObject();
   // create a new Date object using  the above timestamp:
  const date = new Date(timestamp);
    
  // set "unix" value in our result object:
  result.unix = timestamp;
    
  // build "utc" value in result object:
  result.utc = date.toUTCString();

  // send this response back to the browser:
  response.send(result);
};

// 3. If the date_string is empty... :
app.get(endpoint, (request, response) => {
  sendResult(Date.now(), response);
});

app.get(`${endpoint}:date_string`, (request, response) => {

// 2. Check for valid date_string:
  // set timestamp to a variable: sended as a string, so we have to change the type to Number
  let timestamp = + request.params.date_string;
  
  // check if the timestamp is invalid:
  if (!(timestamp) && timestamp !== 0) {
    timestamp = Date.parse(request.params.date_string); // set the timestamp from ISO format
  }
  
  // 5. check if the timestamp still invalid (neighter number nor ISO format...)
  if (!(timestamp) && timestamp !== 0) {
    response.send({
      error: "Invalid Date"
    });
    //console.log(request.params.date_string);
    return;
  }
  
  // 4. From here we have a valid timestamp:
  sendResult(timestamp, response);
});

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log(`Your app is listening on port ${listener.address().port}`);
});