Node.js/Express.js 应用程序仅适用于端口 3000

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18008620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:11:16  来源:igfitidea点击:

Node.js/Express.js App Only Works on Port 3000

javascriptnode.jsconfigurationexpressport

提问by Benjamin Martin

I have a Node.js/Express.js app running on my server that only works on port 3000 and I'm trying to figure out why. Here's what I've found:

我有一个 Node.js/Express.js 应用程序在我的服务器上运行,它只能在端口 3000 上运行,我正在尝试找出原因。这是我发现的:

  • Without specifying a port (app.listen()), the app runs but the web page does not load.
  • On port 3001 (app.listen(3001)) or any other port that is not in use, the app runs but the web page does not load.
  • On port 2999, the app throws an error because something else is using that port.
  • On port 3000, the app runs and the web page loads fine.
  • 不指定端口 ( app.listen()),应用程序运行但网页不加载。
  • 在端口 3001 ( app.listen(3001)) 或任何其他未使用的端口上,应用程序运行但网页不加载。
  • 在端口 2999 上,应用程序抛出错误,因为其他东西正在使用该端口。
  • 在端口 3000 上,应用程序运行并且网页加载正常。

I know that Express apps default to port 3000. But strangely, my app only runs when I explicitly make it run on port 3000 (app.listen(3000)).

我知道 Express 应用程序默认使用端口 3000。但奇怪的是,我的应用程序仅在我明确让它在端口 3000 ( app.listen(3000))上运行时才运行。

I found this on line 220 of /usr/bin/express:

我在第 220 行找到了这个/usr/bin/express

app.set(\'port\', process.env.PORT || 3000);

Which is doing as previously stated: setting the port to what is specified or to 3000 if nothing is specified.

如前所述:将端口设置为指定的端口,如果未指定任何内容,则设置为 3000。

How could I make my app work on a different port such as 8080 or 3001?

我怎样才能让我的应用程序在不同的端口上工作,比如 8080 或 3001?

Thanks!

谢谢!

Edit: Code Sample (Very Simple Node/Express App)

编辑:代码示例(非常简单的节点/Express 应用程序)

var express = require("express");
var app = express();

app.get('/', function(req, res){
    res.send('hello world'); 
});

// Only works on 3000 regardless of what I set environment port to or how I set [value] in app.set('port', [value]).
app.listen(3000);

回答by EhevuTov

The following works if you have something like this in your app.js:

如果您的 app.js 中有类似内容,则以下内容有效:

http.createServer(app).listen(app.get('port'),
  function(){
    console.log("Express server listening on port " + app.get('port'));
});

Either explicitly hardcode your code to use the port you want, like:

要么明确硬编码您的代码以使用您想要的端口,例如:

app.set('port', process.env.PORT || 3000);

This code means set your port to the environment variable PORTor if that is undefinedthen set it to the literal 3000.

此代码意味着将您的端口设置为环境变量,PORT或者如果将undefined其设置为文字3000.

Or, use your environment to set the port. Setting it via the environment is used to help delineate between PRODUCTIONand DEVELOPMENTand also a lot of Platforms as a Service use the environment to set the port according to their specs as well as internal Express configs. The following sets an environment key=value pair and then launches your app.

或者,使用您的环境来设置端口。通过环境设置它用于帮助划分PRODUCTIONDEVELOPMENT许多平台即服务使用环境根据其规范以及内部 Express 配置设置端口。下面设置一个环境键=值对,然后启动您的应用程序。

$ PORT=8080 node app.js

In reference to your code example, you want something like this:

参考您的代码示例,您需要这样的东西:

var express = require("express");
var app = express();

// sets port 8080 to default or unless otherwise specified in the environment
app.set('port', process.env.PORT || 8080);

app.get('/', function(req, res){
    res.send('hello world');
});

// Only works on 3000 regardless of what I set environment port to or how I set
// [value] in app.set('port', [value]).
// app.listen(3000);
app.listen(app.get('port'));

回答by Larry Lu

In bin/www, there is a line:

bin/www,有一行:

var port = normalizePort(process.env.PORT || '3000');

Try to modify it.

尝试修改它。

回答by John Williams

Try this

尝试这个

$ PORT=8080 node app.js

回答by Abhishek Gautam

Refer to this link.

请参阅此链接。

Try to locate the bin>www location and try to change the port number...

尝试定位 bin>www 位置并尝试更改端口号...

回答by AliAvci

The default way to change the listening port on The Express framework is to modify the file named www in the bin folder.

在 Express 框架上更改监听端口的默认方式是修改 bin 文件夹中名为 www 的文件。

There, you will find a line such as the following

在那里,您会找到如下一行

var port = normalizePort(process.env.PORT || '3000');

Change the value 3000 to any port you wish.

将值 3000 更改为您希望的任何端口。

This is valid for Express version 4.13.1

这对 Express 版本 4.13.1 有效

回答by user3711307

Just a note for Mac OS X and Linux users:

Mac OS X 和 Linux 用户的注意事项:

If you want to run your Node / Express app on a port number lower than 1024, you have to run as the superuser: sudo PORT=80 node app.js

如果要在低于 1024 的端口号上运行 Node / Express 应用程序,则必须以超级用户身份运行: sudo PORT=80 node app.js

回答by user1276682

In the lastest version of code with express-generator (4.13.1) app.js is an exported module and the server is started in /bin/www using app.set('port', process.env.PORT || 3001) in app.js will be overridden by a similar statement in bin/www. I just changed the statement in bin/www.

在带有 express-generator (4.13.1) 的最新版本的代码中,app.js 是一个导出模块,服务器在 /bin/www 中使用 app.set('port', process.env.PORT || 3001) 启动在 app.js 中将被 bin/www 中的类似语句覆盖。我刚刚更改了 bin/www 中的语句。

回答by user2855232

Noticed this was never resolved... You likely have a firewall in front of your machine blocking those ports, or iptables is set up to prevent the use of those ports.

注意到这从未解决过...您的机器前面可能有防火墙阻止了这些端口,或者 iptables 设置为阻止使用这些端口。

Try running nmap -F localhost when you run your app (install nmap if you don't have it). If it appears that you're running the app on the correct port and you can't access it via a remote browser then there is some middleware or a physical firewall that's blocking the port.

运行应用程序时尝试运行 nmap -F localhost(如果没有,请安装 nmap)。如果看起来您在正确的端口上运行应用程序并且您无法通过远程浏览器访问它,那么有一些中间件或物理防火墙阻止了该端口。

Hope this helps!

希望这可以帮助!

回答by dinigo

The line you found just looks for the environmental variable PORT, if it's defined it uses it, otherwise uses the default port 3000. You have to define this environmental variable first (no need to be root)

您找到的行只是查找环境变量PORT,如果已定义,则使用它,否则使用默认端口3000。你必须先定义这个环境变量(不需要是root)

export PORT=8080
node <your-app.js>

回答by MEAbid

If you want to show something you're connected on 3000

如果你想展示你在 3000 上连接的东西

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

I hope that will be helpful to you

我希望这会对你有所帮助