Handling Empty Request Parameters

Here’s my code for handling empty date request parameter.

// handler for timestamp api endpoint
app.get("/api/timestamp/:date", function(req, res) {
  // handle endpoint if date is empty
  if (!req.params) {
    res.json({"unix": new Date().getTime(), "utc": new Date().toUTCString()});

I don’t know why it doesn’t work when I request with no date parameter.

What do you mean by “doesn’t work”. Do you have a link to your project?

Here’s the link to my project.

What do you mean by “doesn’t work”? Be as detailed as possible.

I get this error: Cannot GET /api/timestamp

You need to add a ? after :date in the route.

Once you fix that, you should probably add the following before your if statement. You will probably be surprised at the value shown.


Yea, it works now. Now why is that? Is it like a regular expression?

Basically. https://expressjs.com/en/guide/routing.html#route-paths

1 Like