11821

Passing unknown number of params to NodeJS routes

Question:

How can I pass an unknown number of params to NodeJS routes, and save them to an array?:

http://127.0.0.1:3000/param1/param2/param3...

I thought about doing:

app.get('/*', myHandler);

And just tokenize the string, but there should be a better way.

Answer1:

It looks like you are using express routing in NodeJS. If so, you might want to experiment with the regular expressions. Like the following will give you the full path (/vararg/) in req.params[0] but there may be a better way:

app.get(/^\/vararg\/(.*)/, function(req,res) { res.send("Got parameters : " + req.params[0]); })

So if I do a get on localhost:3000/vararg/foo/bar/gar it will send:

<blockquote>

Got parameters : foo/bar/gar

</blockquote>

Or localhost:3000/vararg/foo will produce:

<blockquote>

Got parameters : foo

</blockquote>

And then you can split the parameter into the other parts.

Recommend

  • VHDL indexed name issue
  • Add a border at the bottom of the menu on hover and active
  • Gradle sync failed: Cause: compileSdkVersion is not specified
  • Google Charts: Creating summary charts after category filters have been applied (+ JSFiddle issue)
  • Running shell script from PHP as different (non root) user
  • Send mail with file attachment
  • How to display a specific attribute name and value beside a node in JTree?
  • Ag-Grid expand row
  • Follow-up: Matching factor levels in R with shared unique row names
  • Estimote SDK for Ecilipse
  • How to check the health of application gateway in Azure
  • How to get the new coordinates from Angular Google Maps? (Angular 6)
  • Text appearance, inside TextView and CardView
  • Determine Azure IP Address of appService, blob and general storage
  • How to check the permissions as per authenticated user using Django and Python
  • Can you recommend a good php Gantt charting development?
  • get_permalink for json_decode not working
  • Java Regex Finding digits in a String
  • Encounter error “IB API required” when IB API is installed
  • SQL function not working when trying to write table to non-default schema
  • Tableview make specific cell or row editable
  • Launch Dash from Jupyter Notebook
  • Authorize Attribute Authentication with Postman in Web Api
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • Multiplying polynomials/simplifying like terms
  • How to define something in JavaScript [closed]
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • Neo4j…how to get a visual representation of my data?
  • Bitrate JWplayer
  • Accessing Arguments, Workflow Variables from custom activities