Timestamp/api creation

Tell us what’s happening:
Hello, Friends…!
my code can’t pass a validation … Someone to tell me why…?
Your code so far

let express = require("express")
const app = express()

app.get('/', (req, res) => {
    res.sendFile(__dirname + "/views/index.html")
})
app.use(express.static(__dirname + "/public"))
app.get('/api/timestamp/:data?', (req, res) => {
    let {
        data
    } = req.params
    if ((/^\d{1,4}-\d{1,2}-\d{1,2}$/).test(data) || (/^(\+||-)\d+\d*$/).test(data)) {
        let parts = data.split('-')
        if ((/^\d{1,4}-\d{1,2}-\d{1,2}$/).test(data)) {
            if ((parts[1] * 1) > 12 || (parts[2] * 1) > 31) {
                res.send({
                    "error": "invalid data"
                })
            }
        }

        res.send({
            unix: new Date(data).getTime() || data,
            utc: (/^(\+||-)\d+\d*$/).test(data) ? new Date(data * 1).toUTCString() : new Date(data).toUTCString()
        })
    } else if (!data) {
        res.send({
            unix: new Date().getTime(),
            utc: new Date().toUTCString()
        })
    } else {
        res.send({
            "error": "invalid data"
        })
    }
})

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Timestamp Microservice

Link to the challenge:

Hello! Welcome to the community :partying_face:!

I’ve edited your post 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 (’).

1 Like

Could you share your full code (if it is not) on repl.it, glitch.com or github.com?

OK. thanks you …it’s my first post on this forum…

1 Like

it’s the link of my repl : https://repl.it/@AlassaniMoukadi/boilerplate-project-timestamp

1 Like

This is weird. I think there’s a problem with FCC right now, since I tested your API with different tools and it works.

For some reason, FCC doesn’t get a response (not even a simple object), but tools like https://reqbin.com/ do.

I’ll investigate further and let you know if I find something. Sorry for the inconvenience.

1 Like

thank you very much. i feel better knowing that my code not really in pain as it seems…

1 Like

In the end, it was a problem with your code :sweat_smile:.

There are some libraries that need to be setup before others. In your case, the cors was setup after the routes were defined, which caused the problem. Of course, I forgot about it and didn’t notice immediately :sweat_smile:.

Solution

Move the cors setup to the top of the file, just after creating an instance of express, like this:

const app = express();
const cors = require("cors");

app.use(
  cors({
    optionSuccessStatus: 200
  })
);

You have some logical errors too, but I’ll let you find out :stuck_out_tongue:.

Hint: res.send() or res.json() doesn’t stop the execution of the function.

1 Like

ok… i going for it now … thanks

1 Like