简体   繁体   English

Node.js + Express仅在测试服务器上的端口3000上工作

[英]Node.js + Express only work on port 3000 on test server

On Local Test it work. 在本地测试中可以正常工作。 on different port (3001, 8080) 在不同的端口上(3001、8080)

But on Test Server (Azure) 但是在测试服务器上(Azure)

I run 2 instance of Node App on same machine 我在同一台机器上运行2个Node App实例

$ node api1/index.js (on port 3000)
$ node api2/index.js (on port 3001)

and

$ node api1/index.js (on port 3001)
$ node api2/index.js (on port 3000)

But it only works on port 3000. 但它仅适用于端口3000。

How do I set different port in Express? 如何在Express中设置其他端口?

Now, I've changed at app.listen(3001) on index.js and it doesn't work. 现在,我已在index.js的app.listen(3001)上进行了更改,但它不起作用。

Often cloud platforms set an environment variable that contains the port they want you to stick your app on. 通常,云平台会设置一个环境变量,其中包含它们希望您将其粘贴在其上的端口。 I don't have any experience with Azure... See the answer here: How to run a node.js server on Azure? 我没有使用Azure的任何经验...请参见此处的答案: 如何在Azure上运行node.js服务器?

Specifically: 特别:

var port = process.env.port

Most cloud providers in my experience don't let you play on other ports. 根据我的经验,大多数云提供商都不允许您在其他端口上玩。 You can always specify a localhost port too, though by doing this: 您也可以始终指定一个本地主机端口,尽管这样做是:

var port = process.env.port || 3001 //(or whatever)

app.listen(port);

this way if process.env.port is undefined (which it will be in your dev environment) you fallback to 3001. 这样,如果未定义process.env.port(它将在您的开发环境中),则您将退回到3001。

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM