5

Im trying to handle an empty route parameter if a path is not specified , I would like to return a new date if the route param is empty. So far server is responding like this: Cannot GET /api/timestamp/

app.get("/api/timestamp/:date_string", function(req,res){

let dateString=req.params.date_string
 if(!req.params.date_string){
    dateString=new Date()
    res.json({"unix": dateString.getTime(), "utc" : dateString.toUTCString()})
  }  

})

Currently the server is not responding with a json new date as expected, anyone knows how to catch an empty route param?

0

3 Answers 3

6

Express uses path-to-regexp, so you can check out that documentation here: https://www.npmjs.com/package/path-to-regexp#optional

You can put question mark at the end of parameters to make them optional parameters, like so .../timestamp/:date_string?

Sign up to request clarification or add additional context in comments.

7 Comments

Where do you see it documented that a ? at the end of a param makes it optional? I can't find that in the Express doc for parameters.
if you ctrl f for regex, its mentioned briefly, not really in the context of optional tough, They may assume we know this because we know regex. So you might have to combine our two answers.
so express using path-to-regex and they tell you some more about it npmjs.com/package/path-to-regexp
It looks like the relevant doc is here.
Express already uses path-to-regexp. You don't need to specifically use it yourself. I had upvoted your answer, but now you are suggesting the OP has to use an additional package and that is NOT correct. This is built into Express already. It is already an Express feature.
|
3

In your case you simply use:

app.get("/api/timestamp/:date_string?", function(req,res){

let dateString=req.params.date_string
 if(!req.params.date_string){
    dateString=new Date()
  }  
  res.json({"unix": dateString.getTime(), "utc" : dateString.toUTCString()})
})

You can simply check any variable or value exists or not,

if(variable){
   // this condition executed only when the variable has value.
}

if(variable) returns true when the giver variable is not

  • Undefined
  • null
  • NaN

AND

it also has some value

Comments

1

You do well in checking if your parameter exists. But you have your response only in that if block. I moved it out of the if block so that you will always send a response. Noy only if req.params.date_string doesn't exist.

Edit: I added furthermore the question mark to indicate that your URL parameter is optional, as pointed out in the answer from justin.

app.get("/api/timestamp/:date_string?", function(req,res){
let dateString=req.params.date_string
 if(!req.params.date_string){
    dateString=new Date()
  }  
  res.json({"unix": dateString.getTime(), "utc" : dateString.toUTCString()})
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.