How to get port # when using iisnode?

I have a simple server that works fine (below), the problem is that the process.env.PORT value is not a number, as I expected, it is rather a string value, for example "\\. \ Pipe \ d226d7b0 -64a0-4d04 -96d4-a75e1278b7a9 ". How to get the actual numeric value of the port that the HTTP listener uses?

var path = require("path"); var server = require("../server/server.js"); server.start(process.env.PORT, function() { console.log("Server started."); }); 
+4
source share
2 answers

Unfortunately, you cannot get the port number, since IISNode does not use ports and instead uses streams with channels, which are the random string that you see for the port number. If you want to capture the actual HTTP port on which the IIS website is running, you need to either analyze the configuration file that stores this information, or set it as an environment variable accessible by Node itself.

Is there a reason why you need a port for your application? You do not need the port to run the application, as Node will treat this random string as a channel instead of starting the server on the port.

+2
source

answare is incorrect. You can dynamically set the port number in your application using IIS server variables. https://msdn.microsoft.com/en-us/library/ms524602%28v=vs.90%29.aspx

Just add webconfig to the tag: iisnode promotionServerVars = "SERVER_PORT" then in your express route define:

var portnumber = (req.headers ['x-iisnode-server_port'] || 3000)

res.locals.serverport = port number

this way it will use any application for the iis port number or it works offline, it will use port 3000.

Cheers Gesa

-1
source

Source: https://habr.com/ru/post/1485203/


All Articles