Home > OS >  How to manage empty's get param on nodes router?
How to manage empty's get param on nodes router?

Time:07-14

I do request from an Angular (13) app to a NodeJs server, this way:

getJobData(selectedCandle, bypassLoader = false) {
    return this.http.get(`${this.endpoint}/getJobData/${selectedCandle}`, { context: bypassLoaderContext(bypassLoader) });
}

And I intercept them on NodeJs with Express:

router.get('/getJobData/:selectedCandle(.{0,})', helpers.restAuthorize, async (req, res) => {
}

Problem is: if selectedCandle is empty, it doesn't found the route. It only works when selectedCandle is not "". How can I deal both situation? - i.e. simply pass empty if its empty or value if its valued?

CodePudding user response:

You can just add a question mark at the end of the parameter to make it optional i.e.

router.get('/getJobData/:selectedCandle(.{0,})?', helpers.restAuthorize, async (req, res) => {
}
  • Related