Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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) => {
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading