How can I know port number at the function node

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.

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

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

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:

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.

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.

Or simply...

image

image

1 Like

Wow Thank you very much :heart_eyes: But -_-;;

Yes it is..

But Not work ${PORT} ,

setting.js

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

Thank you But.. NOT work 1884 port..

node-red  -s settings.js -p 1884

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

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

Not solved ..yet ~~ -_-;

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

set PORT=1884

Before you start Node-RED

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

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