Back End Development and APIs Projects - Timestamp Microservice

Tell us what’s happening:

I’ve been unable to complete the challenge due to what i believe to be a bug on the last two topics as I display the date as requested using different methods.

Your code so far

// 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
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/api/:date_string?", (req, res) => {
  const dateString = req.query.date_string;

  // If no date_string is provided, return the current date
  if (!dateString) {
    const currentDate = new Date();
    return res.json({
      unix: currentDate.valueOf(),
      utc: currentDate.toUTCString()
    });
  }

  if (/\d{5,}/.test(dateString)) {
    // Handle Unix timestamp
    let dateInt = parseInt(dateString);
    res.json({
      unix: dateInt,
      utc: new Date(dateInt).toUTCString()
    });
  } 
  else {
    // Handle date string
    let dateObject = new Date(dateString);
    if (dateObject.toString() === "Invalid Date") {
      res.json({ error: "Invalid Date" });
    } 
    else {
      res.json({
        unix: dateObject.valueOf(),
        utc: dateObject.toUTCString()
      });
    }
  }
});

// your first API endpoint... 
app.get("/", function (req, res) {
  res.sendFile(__dirname + '/views/index.html');
});

// Listen on port set in environment variable or default to 3000
var listener = app.listen(process.env.PORT || 3000, function () {
  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/128.0.0.0 Safari/537.36 OPR/114.0.0.0 (Edition std-2)

Challenge Information:

Back End Development and APIs Projects - Timestamp Microservice

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Just a correction I forgot to add, as the definition should be:
const dateString = req.params.date_string;
but the error persists.

you can edit your post or post again your complete code, it helps who is trying to help you

Thank you for the tips I will keep them in mind for future posts, appreciate the help.
I wasn’t able to find where to edit the post so I replied to try to remedy it a little.