How to load JSON resources?

Hi,

I have been developping a node to get the Holidays at a region in spain. My node settings has 2 properties which are select tags:

  1. Territory
  2. Municipality

The Municipality select changes when the territory select changes. That is it loads different municipalities based on the selected territory.

Up to here everything ok and if I use ajax call to an externa web it wil load OK.

However, to lower internet usage I wished to get the JSON with the municipalities into de resources folder of a node. On my local machine, only running node red everything seems OK. BUT when I run the same Node in Home Assistant I get a 404 error as it is trying to get the resources from http://localhost:8123 and not from port 1880 (Where Node RED is running).

I could hardcode the port, but if I would change the port it would stop working (Tried on my local machine running node-red -p 1885).

Is there anyway I could manage to do an ajax call to my resources with the correct host, port and protocol (http/https)? I have not figured out how to get that information from the node .html file.

Thank you in advance.

Can you construct the ajax call URL using
window.location.origin + /resources/<name-of-module>/<path-to-resource>

Hi UnborN,

Thank you for your reply.

Disgratefuly it won't work. window.location has the uri of Home Assistant http://[hostname]:8123/... So when dealing with it I do not have the port (Neither the correct protocol if it has changed) of Node Red which is currentlly running on 1880 (However it may be changed). I have also tried through RED.settings.uiPort but it is not set (Neither will it work whe running node-red -p 1885). Also made a desperate attempt using process but it is not available on the client side (as this is in the ,html file).

I have also tried including a tag to get a javascript from the resources with the relative url with ony an alert('I am here')... Curiosly the javascript wil show the alert but even trying there everything I have thought off it keesrequestig the wrong URL and handing out a 404.

By the way, the broken code is on: JPG-Consulting/node-red-contrib-holiday-euskadi at v0.0.3 (github.com)

It works well on a standalone Node RED but not on Home Assistant (Raspberry Pi Image) all due to the host, port (Maybe even protocol) it gets on that setup.

You should be able to get the Node-RED host values from the settings which should be available to your editor html file as they are attached to the RED object.

So if you can't do that, it implies that Home Assistant is messing with it.

Unfortunately, that would make it a home assistant issue and you might get better help from their forum?

I have started considering part of the problem could be from Home Assistant.

However the problem is not 100% there as I am running Node RED locally while developping. As mentioned I started my local Node RED setting the port: node-red -p 1885. Node RED starts listening on that port and I may connect to it, however, RED.settings has no information about the running port (or I have been unable to locate such information), neithr I find the IP address information (or 0.0.0.0 as specified whn listening to all IP Addresses), or the protolo (http/s)... only the base path ('/').

However, it is true that if I use the relative path in the $.ajax call it retrieves the node resources while this does not happen in Home Assistant (I guess it is due to the fact the same protocol, host and port is used in both connections and it may be retrive through window.location).

Let me double-check that. I've a test node that I can amend.

I need to get lots of info like that for UIBUILDER but I don't always remember how I've done things.


OK, done some quick checks. I can indeed confirm that neither RED.settings.uiPort nor RED.settings.get('uiPort') return anything when called from a node's HTML file.

So I think the only way got get the data is from the runtime. You need a reference to the Admin ExpressJS server that will have the information available.


Confirmed that RED.settings.uiPort is available in the runtime, no need to access the admin expressjs server. You can pass this to the Editor by making use of the settings property on the RED.nodes.registerType options.