# Fails to retrieve the hostname when creating a node

**URL:** https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220
**Category:** Developing Nodes
**Created:** [2 January 2024 19:03 UTC](https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220 "2024-01-02T19:03:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Mediator68](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mediator68/32/78940_2.png) [@Mediator68](https://discourse.nodered.org/u/Mediator68)
#### Post date: [2 January 2024 19:03 UTC](https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220/1 "2024-01-02T19:03:12Z")

</div>

Hi,  
Happy new year 😄  
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](https://discourse.nodered.org/uploads/short-url/xVZJ6kO6tpaazSklrpD9q1GvTXZ.log) (4.7 KB)  
[index\_js.log](https://discourse.nodered.org/uploads/short-url/l6m5b03lG3PUC6mAkQJ5NB7LUAM.log) (3.4 KB)

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [2 January 2024 19:08 UTC](https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220/2 "2024-01-02T19:08:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Mediator68](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mediator68/32/78940_2.png) [@Mediator68](https://discourse.nodered.org/u/Mediator68)
#### Post date: [2 January 2024 19:40 UTC](https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220/3 "2024-01-02T19:40:45Z")

</div>

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

Thank you

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [2 January 2024 20:01 UTC](https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220/4 "2024-01-02T20:01:41Z")

</div>

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

```auto
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

```auto
$.getJSON(`my-kick-ass-node-prefix/hostname`, (data) => {
   // data.hostname
})

```

---

<div class="post-metadata">

### Author: ![Mediator68](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mediator68/32/78940_2.png) [@Mediator68](https://discourse.nodered.org/u/Mediator68)
#### Post date: [2 January 2024 20:54 UTC](https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220/5 "2024-01-02T20:54:11Z")

</div>

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. 👌

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [16 January 2024 20:55 UTC](https://discourse.nodered.org/t/fails-to-retrieve-the-hostname-when-creating-a-node/84220/6 "2024-01-16T20:55:01Z")

</div>

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