process.env.PORT is undefined

回眸只為那壹抹淺笑 提交于 2019-12-11 00:44:07

问题


I am trying to go to production on one of my node.js applications which runs ion windows server 2012. After a week of troubleshooting and reading everything I could get to online, the problem seems to be the "process.env.PORT" variable.


The symptoms are as follows:

The website works fine when:

set PORT=3510 in console
domain.com:3510/index.html in remote browser

However, when I type:

domain.com

I get a 404 - File or directory not found.


When I console.log the "process.env" object at startup of my server.js, the console.log prints an object with several properties but no "PORT" property (unless I "set PORT=3510"). Without setting the port manually prior to launching the server.js, "process.env.PORT" console.logs reports undefined.

However, If manually specify the port, the server still does not work because, according to what I read, node expects "process.env.PORT" to be a named pipe and not a number.

So the problem is very specific:

How do I set the "process.env.PORT variable" on windows 2012 server so that nodejs can work with it?


回答1:


You need to set it in the web.config file. You can set a key as "PORT" with the value you need. See the below web.config file as an example...

<configuration>
    <appSettings>
        <add key="PORT" value="3510" />
    </appSettings>
    ...(Other configurations here)
</configuration>


来源:https://stackoverflow.com/questions/45550262/process-env-port-is-undefined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!