How to set the output-property of the file-read node with a function node?

For a multipage node-red webserver, serving stored html pages and images, the function node should set the output-property of the file-read node to text or buffer, depending on reading and serving html or images.

Now I use 2 file-read nodes with different output, one for text and one for images and that works but if possible I would prefer to use only 1.

How to set that property from a function node placed in the flow before the file-read node? Thx.

Something like this:

[{"id":"c9d33f11764714ae","type":"switch","z":"dd743d35a9e71eef","name":"","property":"filename","propertyType":"msg","rules":[{"t":"cont","v":".html","vt":"str"},{"t":"cont","v":".png","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":490,"y":140,"wires":[["94949fd7249f3f39"],["4e9ae86fc66fe2bf"]]},{"id":"94949fd7249f3f39","type":"file in","z":"dd743d35a9e71eef","name":"output text","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":670,"y":100,"wires":[["ec66ab58b3bc740e"]]},{"id":"4e9ae86fc66fe2bf","type":"file in","z":"dd743d35a9e71eef","name":"output buffer","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":670,"y":180,"wires":[["ec66ab58b3bc740e"]]},{"id":"c5c13671f4cd820f","type":"inject","z":"dd743d35a9e71eef","name":"/home/pi/temp/index.html","props":[{"p":"filename","v":"/home/pi/temp/index.html","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","x":250,"y":120,"wires":[["c9d33f11764714ae"]]},{"id":"cf265fa629e194e7","type":"inject","z":"dd743d35a9e71eef","name":"/home/pi/temp/raspberrypi.png","props":[{"p":"filename","v":"/home/pi/temp/raspberrypi.png","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","x":240,"y":160,"wires":[["c9d33f11764714ae"]]},{"id":"ec66ab58b3bc740e","type":"debug","z":"dd743d35a9e71eef","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":140,"wires":[]}]

Scratch that, I misread the question.

Thanks for answering, yes this way I could solve it also, I am new to node-red , so I am not sure if and how every property of a node can be set by a previous one in the flow.
For example the filename-property of the file-read node could be set, so I was wondering why the output-property could not be set.. but all I tried to do that did not work.
Can all properties be set in theory?

If you look at the help text for the node in the right hand pane after clicking the node, it should tell you what properties you can pass in.

Thank you, I guess it are the properties in the read color under the heading Inputs that you mean?
That means only the filename can be set by other nodes in the flow?

Strange that not all properties can be set, this forces me to use 2 nodes on 2 outputs at the function.

What is the difference between the two?

I would think it is good practice to let all properties be able to be set by the incoming message, but that is not always the case.

You could create a subflow, with the switch node and 2+ file in nodes from my example that would handle the task though.

This should handle most files:

[{"id":"f59e3fa306dac4d1","type":"subflow","name":"Auto File In","info":"","category":"","in":[{"x":80,"y":120,"wires":[{"id":"c9d33f11764714ae"}]}],"out":[{"x":520,"y":120,"wires":[{"id":"94949fd7249f3f39","port":0},{"id":"4e9ae86fc66fe2bf","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"c9d33f11764714ae","type":"switch","z":"f59e3fa306dac4d1","name":"","property":"filename","propertyType":"msg","rules":[{"t":"cont","v":".htm","vt":"str"},{"t":"cont","v":".js","vt":"str"},{"t":"cont","v":".css","vt":"str"},{"t":"cont","v":".php","vt":"str"},{"t":"cont","v":".txt","vt":"str"},{"t":"cont","v":".png","vt":"str"},{"t":"cont","v":".jpg","vt":"str"},{"t":"cont","v":".jpeg","vt":"str"},{"t":"cont","v":".bmp","vt":"str"},{"t":"cont","v":".gif","vt":"str"},{"t":"cont","v":".tif","vt":"str"},{"t":"cont","v":".raw","vt":"str"}],"checkall":"true","repair":false,"outputs":12,"x":190,"y":120,"wires":[["94949fd7249f3f39"],["94949fd7249f3f39"],["94949fd7249f3f39"],["94949fd7249f3f39"],["94949fd7249f3f39"],["4e9ae86fc66fe2bf"],["4e9ae86fc66fe2bf"],["4e9ae86fc66fe2bf"],["4e9ae86fc66fe2bf"],["4e9ae86fc66fe2bf"],["4e9ae86fc66fe2bf"],["4e9ae86fc66fe2bf"]]},{"id":"94949fd7249f3f39","type":"file in","z":"f59e3fa306dac4d1","name":"output text","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":370,"y":80,"wires":[[]]},{"id":"4e9ae86fc66fe2bf","type":"file in","z":"f59e3fa306dac4d1","name":"output buffer","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":370,"y":160,"wires":[[]]},{"id":"b369c52d22fe8e01","type":"subflow:f59e3fa306dac4d1","z":"10aa4c3ab6ad18a1","name":"","x":530,"y":380,"wires":[[]]}]

One file read node has it's output property set to string and the other file read node has it's output property set to buffer. If I not modify the output property the image is not send correctly by the http response node.

Yes that looks good and covers all! Thanks.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.