I am creating a small http admin client. The client is implemented as a Node-Red flow. This works fine, but I have to manually configure the url of the http admin api. I would like to access the admin api of the same Node-Red engine as the flow runs in, so in principle I should not really have to manually enter the api url.
Passing in the url to the http request dynamically works fine, but is there some way to find out what the url should be using some existing node types? Is there any property on the RED object that could be used to retrieve the url? Can you get this information from inside a function node?
If you are writing a program to call services on the same host, you use the ’localhost’ as address, and do not need to configure any ip adress or DNS hostname. In the case of an http service you also need to know the port number and path to the service. The question is if you could pick up this information from the running Node-Red server - in order NOT to have to MANUALLY enter it.
That would not help, you still would have to populate the information manually. The only context where this is known is inside the Node-Red server, so if it can not be retrieved from any api provided to the server nodes, then it could not be done.
Is there an api provided to Node-Red nodes, where the node can ask for the url used for contacting the local http admin service, where local means the service of the Node-Red instance where the asking node resides ?
Honestly what you are asking is not clear to someone without your setup or details of knowledge or task at hand.
For example:
Are you talking about the node-red admin API or an API you have created in node-red using http-in nodes?
UPDATE (following your follow up post)
Unless you specify a port, it will be 1880. But if you do specify a different port (via start command or service or settings.js), do it via an env var, then your other application can use that.
Not that I recall, but you might get it from the Node-RED admin API for diagnostics but you'd still need to know the port and (maybe) the binding IP
There may be another way - like call an own API (hrtp-in~>http-reply) from within the node-red instance and inspect the req object, write that to somewhere on the file system or MQTT or DB or other common area for retrieval by other application.
Not got any screen time today to look into it for you. Someone else will probably pop in and have a better idea.
@helander I don't know of (inside node-red flows) of a way. I know you can get this info via a plugin or custom node and then you'd be able to populate context or surface the values by some other means.
The values you'd want are httpAdminRoot, uiPort and uiHost and they'd be accessible in RED.settings (I think)
You can try a grabbing these in a function node but I'm fairly certain they won't be accessable.
One way to do this would be to specify, for example, the port in settings.js by putting at the start, before the exports line process.env.MYPORT = 1881
and then lower down where the port is defined uiPort: process.env.MYPORT,
Then the port will be available as the env variable MYPORT. Do the same for the path to make it available.
But that is MANUAL configuration and I could equally do that in my application.
Looking into the node-red code, it seems like the port number is just passed to the http server without node-red ever ”touching” it, so node-red is not aware of the port number, and as such there is no way for node-red to provide it to nodes.
I guess there is no Javascript to access the functionality exposed by the admin service (bypassing http)?