In Node-RED events and documents, I often see the many pairs of change node (or function node) and exec node. Because the current exec node supports just msg.payload
to specify a value in the received message. To solve the situation, I would like to suggest that the exec node supports JSONata as typedInput
in the command field.
Current exec node
The following flow is an example that uses pair of the change node and exec node. (Yellow file node is node-red-contrib-browser-utils
module to upload file from the browser)
Flow
[{"id":"c55b3f36.bc62c","type":"http response","z":"6d621a26.d5de24","name":"","statusCode":"","headers":{},"x":630,"y":200,"wires":[]},{"id":"f0087c1c.badd","type":"file","z":"6d621a26.d5de24","name":"save data to file","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":600,"y":100,"wires":[["f7fdb56e.f87f68"]]},{"id":"31494145.0706ae","type":"change","z":"6d621a26.d5de24","name":"msg.payload = <file buffer>\\n msg.filename = 'tmp/<timestamp>.jpg'","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.files[0].buffer","tot":"msg"},{"t":"set","p":"filename","pt":"msg","to":"'tmp/' & $millis() & '.jpg'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":100,"wires":[["f0087c1c.badd"]]},{"id":"7a1eb15.26b465","type":"http in","z":"6d621a26.d5de24","name":"","url":"/fileupload","method":"post","upload":true,"swaggerDoc":"","x":100,"y":100,"wires":[["31494145.0706ae"]]},{"id":"ba523599.4f7898","type":"comment","z":"6d621a26.d5de24","name":"REST API to upload file and handle it using command","info":"","x":220,"y":40,"wires":[]},{"id":"f045dfa1.b84db","type":"comment","z":"6d621a26.d5de24","name":"File upload flow","info":"","x":100,"y":300,"wires":[]},{"id":"c8afa05c.3ecaa","type":"http request","z":"6d621a26.d5de24","name":"file upload","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://localhost:1880/fileupload","tls":"","persist":false,"proxy":"","authType":"","x":430,"y":340,"wires":[["2377f84a.cc7258"]]},{"id":"2377f84a.cc7258","type":"debug","z":"6d621a26.d5de24","name":"ouput md5 value","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":340,"wires":[]},{"id":"b4af66bf.39e728","type":"fileinject","z":"6d621a26.d5de24","name":"","x":100,"y":340,"wires":[["9ea60a13.5f1838"]]},{"id":"9ea60a13.5f1838","type":"function","z":"6d621a26.d5de24","name":"create post data","func":"msg.headers = { 'Content-Type': 'multipart/form-data' };\nmsg.payload = {\n 'file' : {\n 'value': msg.payload,\n 'options': { 'filename': 'file.jpg' }\n }\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":340,"wires":[["c8afa05c.3ecaa"]]},{"id":"c67f51a3.b3467","type":"group","z":"6d621a26.d5de24","name":"Pair of change and exec node","style":{"stroke":"#ff0000","label":true,"label-position":"s","color":"#ff0000"},"nodes":["250e2ea0.fad9e2","f7fdb56e.f87f68"],"x":34,"y":151.5,"w":532,"h":105},{"id":"250e2ea0.fad9e2","type":"exec","z":"6d621a26.d5de24","g":"c67f51a3.b3467","command":"md5sum","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"md5sum + msg.payload","x":430,"y":200,"wires":[["c55b3f36.bc62c"],[],[]]},{"id":"f7fdb56e.f87f68","type":"change","z":"6d621a26.d5de24","g":"c67f51a3.b3467","name":"msg.payload = msg.filename","rules":[{"t":"set","p":"payload","pt":"msg","to":"filename","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":200,"wires":[["250e2ea0.fad9e2"]]}]
exec node using JSONata
If the exec node supports JSONata, the flow doesn't need to have a change node to set msg.payload
before exec node.
Image of exec node property with typedInput
Additionally, users can specify more than two variables using JSONata in the exec node.