Fails to retrieve the hostname when creating a node

Hi,
Happy new year :smile:
I have creating a node, I use 2 files

  • index.js
  • index.html

I had no problems with the installation of the node but i use API to load widget in my editor and when i launch my node.
I have to find the DNS localhost of my server to call my API.
In Index.js i used
os = require("os");
hostName = os.hostname();
that's work fine but in the file index.html it doesn't work (in the function oneditprepare).
I’ve read a lot of discussion about it , I tried to use environment variables and add this on settings.js like this.
process.env.HOST_NAME = "eurekapr";
but I failed to recover the value in my file ,index.html.

Can someone help me ?

Thx

I have attached both files

index_html.log (4.7 KB)
index_js.log (3.4 KB)

The html file and oneditprepare function run on the browser client side which is pure JavaScript and doesn’t have access to nodejs things like os. etc If you need things from server side the client has to do a request to get them.

Thanks for your quick reply, I can use environment variables instead ?
How can i do?

Thank you

All of that lives in the 'runtime' - and is not exposed to browsers.

here is what one should do, to pass data from runtime to the bowser

  1. Registrer an admin endpoint in your js file
RED.httpAdmin.get('/my-kick-ass-node-prefix/:action',
    RED.auth.needsPermission('flows.write'),
    (req, res) => {
        if (req.params.action === 'hostname') {
              res.json({'hostname', os.hostname()});
        }
    }
);
  1. Call it from browser (html file) using standard jQuery methods
$.getJSON(`my-kick-ass-node-prefix/hostname`, (data) => {
   //  data.hostname
})
1 Like

it's work fine,
you explained the answer very well, it is true that we do not have access to the server resource thank you very much. :ok_hand:

1 Like

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