I'm Trying to build crud application using express and node.js
example query string
/table/?id=78&title=someTitle&code=3
where ID, Title And Code are optional. meaning:
But the problem is either I have to enter whole url string with null values, or the values mixed together, or express won't recognize my regex pattern (pattern is valid according to https://regexr.com website)
I've tried:
Route:
/table/\??(id=)?:id?\&?(title=)?:title?\&?(code=)?:code?
Path:
/table/?id=78&title=someTitle&code=3
Result:
created my own regex:
\/table\/\??(id=78)?\&?(title=someTitle)?\&?(code=3)?
This will work unless I add optional express parameters.
Path
/table/?id=78
request.params
{ id: '78'}
----
Path
/table/?code=3
request.params
{ code: '3' }
----
Path
/table/?id=78&title=someTitle
request.params
{ id: '78', title: 'someTitle' }
PS: I know I can achieve this with matching regular expression to request.url But I want to know if there is any other way with express.
I can achieve this by using req.query
instead of req.params
Path
/table/?id=78&title=someTitle
request.query
{ id: '78', title: 'someTitle' }