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.

Here’s the link to my project.

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.

console.log(req.params);

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