process.env.PORT未定义 [英] process.env.PORT is undefined

查看:283
本文介绍了process.env.PORT未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在运行ion Windows Server 2012的我的一个node.js应用程序上投入生产.经过一周的故障排除并阅读了所有可以上网的内容后,问题似乎出在"process.env. PORT"变量.

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 website works fine when:

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

但是,当我键入:

domain.com

domain.com

我得到404-找不到文件或目录.

I get a 404 - File or directory not found.

当我在server.js启动时用console.log记录"process.env"对象时,console.log将打印一个具有多个属性但不包含"PORT"属性的对象(除非我设置PORT = 3510").在没有启动server.js之前手动设置端口的情况下,"process.env.PORT" console.logs报告未定义.

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.

但是,如果手动指定端口,则服务器仍然无法工作,因为根据我的阅读,节点希望"process.env.PORT"是命名管道而不是数字.

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.

所以问题很具体:

如何在Windows 2012 Server上设置"process.env.PORT变量",以便nodejs可以使用它?

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

推荐答案

您需要在web.config文件中进行设置.您可以将键设置为具有所需值的"PORT".参见下面的web.config文件作为示例...

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>

这篇关于process.env.PORT未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆