# How can I know port number at the function node

**URL:** <https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597>\
**Category:** General\
**Tags:** function-node\
**Created:** [8 June 2022 06:43 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597 "2022-06-08T06:43:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![simmani5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simmani5/32/57008_2.png) [@simmani5](https://discourse.nodered.org/u/simmani5)\
**Post date:** [8 June 2022 06:43 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/1 "2022-06-08T06:43:11Z")

</div>

I want to recognize the node-red execution argc or arcv option ex) -p port number at the function node.

Why ? I wnat to use one flows.js run it differently with argv or port number like this.

```auto
// ex))
// .node-red>node-red -s settings.js -p 1881 (( for 1st customer DataBase ))
// .node-red>node-red -s settings.js -p 1882 (( for 2nd customer DataBase ))
// .node-red>node-red -s settings.js -p 1883 ((.. 3rd ..))
// .node-red>node-red -s settings.js -p 1884 ((.. 4th .))

```

What can I know executed port number at the function node ?  
get.env("port") ?? First of all, thank you.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [8 June 2022 06:45 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/2 "2022-06-08T06:45:40Z")

</div>

Sorry - trying to understand what you are asking - i think you are saying you want to run multiple instances of NR on a single box on different ports ?

You are then asking can you query from within a flow to see which port that instance is running on ?

Craig

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 June 2022 09:35 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/3 "2022-06-08T09:35:41Z")

</div>

One way would be to add a global variable in settings.js

However, you need to do it in a specific way that requires some changes to how settings.js is structured.

Normally, settings.js is structured like this:

```auto
module.exports = {
    ... all the settings
}

```

What you need to do is move the settings to their own variable and then export that. It lets you reference the content of the variable before it is exported which lets you get hold of things like the port which is dynamically allocated on startup.

```auto
const nrsettings = {
    // ...
    uiPort: process.env.PORT || 1880,
    // ...
} // ---- End of exports ---- //

nrsettings.functionGlobalContext._port = nrsettings.uiPort

module.exports = nrsettings

```

Of course, you will need to test whether setting the port using the `-p` argument sets the port early enough to be picked up.

Of course, using the port setting I've used above would mean that you could set the port using an environment variable and you could also pass the env variable to global.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 June 2022 11:31 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/4 "2022-06-08T11:31:39Z")

</div>

Or simply...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/00f2102d7a90c0f2ecd24d17b25fa397630c484f.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/3953431c65aef17a41896fc6650421d3bcb18975.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/17c058cbda1b45aff51b93be19701e9a058aa386.png)

---

<div class="post-metadata">

**Author:** ![simmani5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simmani5/32/57008_2.png) [@simmani5](https://discourse.nodered.org/u/simmani5)\
**Post date:** [9 June 2022 02:26 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/5 "2022-06-09T02:26:12Z")

</div>

Wow Thank you very much 😍 But -\_-;;

---

<div class="post-metadata">

**Author:** ![simmani5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simmani5/32/57008_2.png) [@simmani5](https://discourse.nodered.org/u/simmani5)\
**Post date:** [9 June 2022 04:42 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/6 "2022-06-09T04:42:44Z")

</div>

Yes it is..

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a4148a69ce5ac4f11caf3b69e7e85440afbeaf52.png)

But Not work ${PORT} ,

setting.js

```auto
process.env.FOO = 'just foo bar';
var PORT = process.env.PORT || 1880 ;
console.log("PORTTTTTTTTTTTTTTTTTTTTTTTTTT="+PORT);

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/885c37f348b4717d5fb5ae782a4498319e96180e.png)

---

<div class="post-metadata">

**Author:** ![simmani5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simmani5/32/57008_2.png) [@simmani5](https://discourse.nodered.org/u/simmani5)\
**Post date:** [9 June 2022 04:46 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/7 "2022-06-09T04:46:27Z")

</div>

Thank you But.. NOT work **1884** port..

```auto
node-red -s settings.js -p 1884

```

it became 1880 ~~~  
How can I do ???

---

<div class="post-metadata">

**Author:** ![simmani5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simmani5/32/57008_2.png) [@simmani5](https://discourse.nodered.org/u/simmani5)\
**Post date:** [9 June 2022 05:15 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/8 "2022-06-09T05:15:45Z")

</div>

--_--;;  
i don't know -_- How to get settings.uiPort

---

<div class="post-metadata">

**Author:** ![simmani5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simmani5/32/57008_2.png) [@simmani5](https://discourse.nodered.org/u/simmani5)\
**Post date:** [9 June 2022 05:23 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/9 "2022-06-09T05:23:09Z")

</div>

Not solved ..yet ~~ -\_-;

---

<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:** [9 June 2022 05:54 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/10 "2022-06-09T05:54:11Z")

</div>

Where are you setting the environment variable ? I would expect to see a line like

set PORT=1884

Before you start Node-RED

---

<div class="post-metadata">

**Author:** ![simmani5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simmani5/32/57008_2.png) [@simmani5](https://discourse.nodered.org/u/simmani5)\
**Post date:** [15 June 2022 10:53 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/11 "2022-06-15T10:53:52Z")

</div>

CMD Dos Promprt Execute -p option  
I do like this..  
c:\Users\Administrator.node-red\>node-red -s setting.js -p 1884  
c:\Users\Administrator.node-red\>node-red -s setting.js -p 1885  
c:\Users\Administrator.node-red\>node-red -s setting.js -p 1886

---

<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:** [14 August 2022 10:54 UTC](https://discourse.nodered.org/t/how-can-i-know-port-number-at-the-function-node/63597/12 "2022-08-14T10:54:06Z")

</div>

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