File writing with the file node

(Yeah, sorry, but I don't get it either)

Ok, next/latest problem that has me stumped.

I want to write to a file (append) in the defined httppublic directory.
(or what ever it is called - but it is set)

This is the code for the sake of keeping it simple:

[{"id":"b52021a0.03338","type":"function","z":"b04360a6.da324","name":"Name of database + timestamp","func":"msg.filename = \"machines/timepi_temp.db\";\nvar time = Date.now();\nvar x = msg.payload;\nmsg.payload = time + ' - ' + x;\nreturn msg;","outputs":1,"noerr":0,"x":1310,"y":840,"wires":[["99a3f963.347218"]]},{"id":"99a3f963.347218","type":"file","z":"b04360a6.da324","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1510,"y":840,"wires":[]},{"id":"edcff7e1.33b628","type":"inject","z":"b04360a6.da324","name":"","topic":"","payload":"clock","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1080,"y":840,"wires":[["b52021a0.03338"]]}]

Now I am not exactly sure what the "file path should be an absolute path, or else it will be relative to ........."

"absolute"?

What does that mean?

If I want to put it in node-red's public directory in "data_base/machines" what is different to the "absolute" path?

Anyway, I run that and nothing happens in the way of errors, but the file isn't made, or I can't find it.

(true at time of posting)

After poking around:
I have httpstatic set to /home/pi/.node-red/public
and I use pictures from there (old story) and got the LCD display working (another story) with the fonts.

So, that part is ok.

Seems...... The file is being made in:
/home/pi/databases/machines/

Why?

What am I missing?

Try this link https://www.google.co.uk/search?q=absolute+file+path&ie=UTF-8&oe=UTF-8&hl=en

1 Like

This should be helpful
https://www.google.com/search?q=what+is+an+absolute+path+to+a+file

1 Like

Ok, so I have since discovered that "relative" paths are entered as:

from:
Relative vs absolute paths.

"../blah/and so on/what ever"

where as absolute are without the leading double dots.

Ok.

BUT!

When I go to this site:
Another version of the same story
They say that ABSOLUTE paths are like:
http://www.mysite.com
and relative paths are like:
/graphics/image.png

But then does on to say

What if you want to link to a file in a folder above the current folder? You have to tell the browser to move up one folder in your relative link by putting two periods and a slash (../) in front of the filename or path:

Which doesn't make sense if I "believe" the first link.

So back to reading.

Not really.

Filing system absolute paths give the entire path from the "root". In Linux, the root of the filing system is always /. In Windows, the root is more complex and starts with a drive designation such as c:\ or \\networkdrive\ (there are also some more complex ones). You can also use / in Windows but it only refers to the root of the current or default drive.

A relative path in filing system terms starts with either , a ./ or a ../ or simply has no leading / at all.

The second site is talking about URL paths rather than operating system paths. The principals are the same but the leading element has to start with protocol://location/. These can get fairly complex.

You will probably want to do some reading on what a URL is and also what a URI is, these are often confused but there is an important difference.

So here's the problem - for me:

This is my RPI settings.js file (part there of)

// that should be served at http://localhost:1880/.
//httpStatic: '/home/nol/node-red-static/',
httpStatic: '/home/pi/.node-red/public',
//httpStatic: '/home/pi',

Don't ask why I have left the line above and the one below. They are "backups" of the settings.

So httpStatic is set on that machine.

I want to write to a file from NR to that part and some more.

Given this code - which I know is not correct:

[{"id":"b52021a0.03338","type":"function","z":"b04360a6.da324","name":"Name of database + timestamp","func":"msg.filename = \"/home/pi/.node-red/public/databases/machines/timepi_temp.db\";\nvar time = Date.now();\nvar x = msg.payload;\nmsg.payload = time + ' - ' + x;\nreturn msg;","outputs":1,"noerr":0,"x":1400,"y":1730,"wires":[["99a3f963.347218"]]},{"id":"99a3f963.347218","type":"file","z":"b04360a6.da324","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1600,"y":1730,"wires":[]},{"id":"edcff7e1.33b628","type":"inject","z":"b04360a6.da324","name":"","topic":"","payload":"clock","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1170,"y":1730,"wires":[["b52021a0.03338"]]},{"id":"e1c53a12.82f458","type":"function","z":"b04360a6.da324","name":"Name of database + timestamp","func":"msg.filename = \"../databases/machines/timepi_temp.db\";\nvar time = Date.now();\nvar x = msg.payload;\nmsg.payload = time + ' - ' + x;\nreturn msg;","outputs":1,"noerr":0,"x":1400,"y":1780,"wires":[["7aa3bcb6.c846bc"]]},{"id":"7aa3bcb6.c846bc","type":"file","z":"b04360a6.da324","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1600,"y":1780,"wires":[]},{"id":"af19af1a.2ae83","type":"inject","z":"b04360a6.da324","name":"","topic":"","payload":"clock","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1170,"y":1780,"wires":[["e1c53a12.82f458"]]}]

The top one works. The other doesn't create the file in the right place.

I've tried all sorts of things trying to get it to "honour" the httpStatic path and append it (as it says in the node info) but can't work out what is going wrong.

I've tried the leading /, no leading / double .. at the start and a few other things.

I think that you are confusing the "current" location for the second example. When NR starts, it will have its own current location that the function node inherits. Trying to second guess relative paths when inside an app is hard which is why you are generally recommended to use absolute paths.

If I remember rightly, NR sets the current folder to your userDir folder, usually /home/pi/.node-red on the Pi if you have a standard installation - though you can move that of course.

So

./public/databases/machines/timepi_temp.db

Would likely be the correct relative location.

Actually it sets it to /home/pi. That is done in the systemd service /lib/systemd/system/nodered.service. If the user that ran the update script is not pi then it is set to that users home directory.

1 Like

Thanks both.

So where does the httpStatic path come into play?

The httpStatic setting has absolutely nothing to do with the File node.

As described before, the httpStatic setting identifies a directory that Node-RED will serve up as static content. That is the only thing it does with that setting.

If you want the File node to write into the directory identified by httpStatic, you'll need to give it the same full path.

So if you have httpStatic set to /home/pi/public and you want the File node to create a file under there, you'll need to make sure the path you give the File node has /home/pi/public at the start.

1 Like

Thanks.

I didn't "get" that originally. Confusion? Maybe. But what ever it is/was, I missed it.

Maybe because when I was asking about that it was for HTTP stuff rather than local stuff.

So there is nothing I can set to make the "home directory for NR" a specified path so when I enter paths, it starts there?

It is possible to edit the nodered.service file to change the working directory of the Node-RED process - as Colin mentioned here.

But, in my opinion, it's better to be explicit and use the full, absolute, path name in your flow.

1 Like

Thanks again.

As I am sure I have said many times.

It isn't I am trying to be difficult. I am just wanting to learn. Sometimes I forget or don't "Get" what is said.

I shall probably stick to the entire path.

It was more just for the sake of checking and wondering if it is/was possible.

I think that is probably the best way to do it anyway.

1 Like