Node-red node for parsing pathparams in routes for RESTful Web Services

567 views
Skip to first unread message

Mike Hill

unread,
Apr 5, 2015, 12:49:03 PM4/5/15
to node...@googlegroups.com
Hello all,

New to Node/Node-Red. I had a hard time figuring out how to get Node-Red to return pathparams similar to the behavior in Express, basically returning key/value pairs on any route with parms defined starting with ":".

Since I couldn't find the answer quickly, build my own node to parse through the route and return key/value pairs in msg.req.route.parms. Probably a better way already exists, but I wanted to get my hands dirty in development and I need to use this functionality a lot...

Thought I'd publish my results and make it available in case it's useful to someone else...



Feedback appreciated.

Mike



Nicholas O'Leary

unread,
Apr 5, 2015, 3:41:22 PM4/5/15
to Node-RED Mailing LIst
Hi Mike,

the msg.req object is the request object straight from Express (3.x) and msg.req.params is the object containing the url parameters you want.

Here's an example that shows it in action:

[{"id":"7ab6512d.8549b","type":"http in","name":"","url":"/api/test/:lastname/:firstname","method":"get","x":167,"y":58,"z":"dbf8b34b.24075","wires":[["6e08ecaa.91f714","5bfe7d89.a40184","8f07ac18.70f85"]]},{"id":"6e08ecaa.91f714","type":"http response","name":"","x":430,"y":124,"z":"dbf8b34b.24075","wires":[]},{"id":"5bfe7d89.a40184","type":"debug","name":"","active":true,"console":"false","complete":"req.params.lastname","x":489,"y":40,"z":"dbf8b34b.24075","wires":[]},{"id":"8f07ac18.70f85","type":"debug","name":"","active":true,"console":"false","complete":"req.params.firstname","x":489,"y":79,"z":"dbf8b34b.24075","wires":[]}]



Nick


--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Hill

unread,
Apr 5, 2015, 8:07:13 PM4/5/15
to node...@googlegroups.com
Nick,

Thanks for the update. I can't believe I missed that. I looked through all of the objects and swear I couldn't find them... Anyway, I've created a redundant node... ;)

Should I pull it from NPM?

Mike

Nicholas O'Leary

unread,
Apr 6, 2015, 4:43:20 AM4/6/15
to node...@googlegroups.com

No problem.... We do need to get some of this better documented, rather than just point at the express docs and leave the user to figure it out.

Up to you what to do with your node... hopefully the exercise of producing it was useful in itself.

Nick


Reply all
Reply to author
Forward
0 new messages