Js/Express.js App Only Works on Port 3000

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

  • 没有指定端口(app.listen()) ,应用程序运行,但网页不加载。
  • 在端口3001(app.listen(3001))或任何其他端口不使用,应用程序运行,但网页不加载。
  • 在端口2999上,应用程序抛出一个错误,因为有其他东西正在使用该端口。
  • 在端口3000,应用程序运行和网页加载罚款。

我知道 Express 应用默认设置为3000端口。但奇怪的是,我的应用程序只有在端口3000(app.listen(3000))上显式运行时才能运行。

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

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

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

我如何让我的应用程序工作在不同的端口,如8080或3001?

谢谢!

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

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);
274108 次浏览

Try this

$ PORT=8080 node app.js

The following works if you have something like this in your 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 PORT or if that is undefined then set it to the literal 3000.

Or, use your environment to set the port. Setting it via the environment is used to help delineate between PRODUCTION and DEVELOPMENT and 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.

$ 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'));

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.

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.

Hope this helps!

Just a note for Mac OS X and Linux users:

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

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.

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

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.

This is valid for Express version 4.13.1

In bin/www, there is a line:

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

Try to modify it.

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)

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

If you want to show something you're connected on 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

Make sure you are running from that folder of your application, where you have the package.json.

Answer according to current version of express

If you talk about the current version of express, if you run app.listen() to start listening without specifying port, Express will chose a random port for your application, to find out about which port it is currently running on use

app.listen(0, () => {
console.log(app.address().port)
}

should output the port of your app. Moreover that first parameter 0 can be totally ignored but is not recommended

Refer to this link.

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

I am using the minimist package and the node startup arguments to control the port.

node server.js --port 4000

or

node server.js -p 4000

Inside server.js, the port can be determined by

var argv = parseArgs(process.argv.slice(2))


const port = argv.port || argv.p || 3000;
console.log(`Listening on port ${port}...`)


//....listen(port);

and it defaults to 3000 if no port is passed as an argument.

You can then use listen on the port variable.

In app.js, just add...

process.env.PORT=2999;

This will isolate the PORT variable to the express application.

I think the best way is to use dotenv package and set the port on the .env config file without to modify the file www inside the folder bin.

Just install the package with the command:

npm install dotenv

require it on your application:

require('dotenv').config()

Create a .env file in the root directory of your project, and add the port in it (for example) to listen on port 5000

PORT=5000

and that's it.

More info here

If you are using Nodemon my guess is the PORT 3000 is set in the nodemonConfig. Check if that is the case.