4

I have a simple server that runs fine (below), the problem is that the value of process.env.PORT is not a number as I expected, rather it is a string value like "\\.\pipe\d226d7b0-64a0-4d04-96d4-a75e1278b7a9". How do I get the actual numeric value of the port the http listener is using?

var path = require("path");
var server = require("../server/server.js");
server.start(process.env.PORT, function() {
    console.log("Server started.");
});
Frank Schwieterman
  • 24,142
  • 15
  • 92
  • 130

2 Answers2

4

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

Is there a reason you need the port for your application? You shouldn't need the port to run the application as Node will treat that random string as a pipe instead of running server on a port.

Kyle Ross
  • 2,090
  • 4
  • 20
  • 27
  • 2
    I'll run the same site on different ports in test environments, and the site needs to know the port to link back to itself. You're right though (so I've been putting the uri authority in a config file). – Frank Schwieterman Jan 03 '14 at 01:55
  • On Azure Web Apps for instance, you don't get to choose what port you want to run on – rather, Azure gives you the port to listen to, so that it's properly mapped to the website. See http://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac/ for example. Now the problem is that Azure uses iisnode, which as you say only gives you the pipe option rather than a numerical port. – David Ammouial Apr 23 '15 at 02:00
0

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

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

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

res.locals.serverport=portnumber

this way it will be using whatever portnumber iis app has, or running standalone mode it will use port 3000.

Cheers Geza

  • The SERVER_PORT variable will only tell you on what port IISNode is listening to, not what port your app should listen to. Unless I misunderstood your answer, the answer by Big Ross seems correct. – David Ammouial Apr 23 '15 at 02:23
  • @David Ammouial, as OP stated in comments, "the site needs to know the port to link back to itself". As the site is actually served by iisnode in this case, the solution by Geza Papp looks like a possible one, though it's not at all nice nor reliable. – Konstantin Dec 21 '16 at 00:13