Node.js / Express.js Application works only on Port 3000

I have a Node.js / Express.js application running on my server that only runs on port 3000, and I'm trying to figure out why. Here is what I found:

  • Without specifying a port ( app.listen() ), the application starts, but the web page does not load.
  • To port 3001 ( app.listen(3001) ) or any other port that is not in use, the application starts, but the web page does not load.
  • On port 2999, the application causes an error because something else is using this port.
  • On port 3000, the application starts and the web page loads normally.

I know that Express applications are by default for port 3000. But, strangely enough, my application only works when I explicitly launch it on port 3000 ( app.listen(3000) ).

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

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

What is being done, as indicated earlier: setting the port to the specified or 3000, if nothing is specified.

How can I configure the application on another port, for example, 8080 or 3001?

Thank!

Edit: sample code (very simple Node / express app)

 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); 
+85
javascript port configuration express
Aug 02 '13 at 4:06 on
source share
16 answers

The following works if there is something like this in your app.js application:

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

Or explicitly specify the code you want to use, for example:

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

This code means that your port matches the PORT environment variable, or if it is undefined , then set it to a literal of 3000 .

Or use the environment to install the port. Configuring it through the environment is used to determine the difference between PRODUCTION and DEVELOPMENT , and many platforms as a service use the environment to set the port in accordance with their specifications, as well as with internal Express configurations. The following sets the environment key pair = value, and then launches your application.

 $ PORT=8080 node app.js 

Regarding your sample code, 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')); 
+105
Aug 02 '13 at 19:29
source share

There is a line in bin/www :

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

Try changing it.

+26
Apr 07 '16 at 2:28
source share

try it

 $ PORT=8080 node app.js 
+12
Aug 02 '13 at 4:09 on
source share

By default, to modify the listening port in the Express platform, a modification of the file named www in the bin folder is used.

There you will find a line such as

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

Change the value of 3000 to any desired port.

This is true for Express version 4.13.1

+9
Jan 17 '16 at 17:12
source share

Refer to this link.

Try to find the cart location> www and try changing the port number ...

+9
Mar 24 '18 at 20:27
source share

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

+4
Sep 17 '15 at 16:47
source share

Just notice for Mac OS X and Linux users:

If you want to run the Node / Express application with a port number below 1024, you must run as root: sudo PORT=80 node app.js

+3
Nov 10 '14 at 2:35
source share

The found string simply looks for the PORT environment variable if it determines that it uses it; otherwise, the default port is 3000 . You must first define this environment variable (no need to be root)

 export PORT=8080 node <your-app.js> 
+2
Aug 4 '16 at 9:12
source share

If you want to show something that you connected to 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 this will be helpful to you.

+2
Jan 29 '17 at 14:23
source share

It was noted that this was never allowed ... You probably have a firewall in front of your machine blocking these ports, or iptables is configured to prevent these ports from being used.

Try running nmap -F localhost when starting the application (install nmap if you don’t have one). If it seems that you are using the application on the correct port and you cannot access it through a remote browser, then there is some middleware or a physical firewall blocking the port.

Hope this helps!

+1
Oct 07 '13 at 15:16
source share

Answer according to the current version of the expression

If you talk about the current version of express , if you run app.listen() to start listening without specifying a port, Express will select a random port for your application to find out which port it uses at startup

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

should output the port of your app . Moreover, the first parameter 0 can be completely ignored, but not recommended

+1
Mar 07 '17 at 18:05
source share

In app.js just add ...

 process.env.PORT=2999; 

This isolates the PORT variable for the express application.

+1
Sep 09 '18 at 2:21
source share

Make sure you work from this folder of your application, where you have package.json.

0
Feb 10 '17 at 10:20
source share

I use the minimist package and host start arguments to control the port.

node server.js --port 4000

or

node server.js -p 4000

Inside server.js port can be defined

 var argv = parseArgs(process.argv.slice(2)) const port = argv.port || argv.p || 3000; console.log('Listening on port ${port}...') //....listen(port); 

and by default 3000 if the port is not passed as an argument.

Then you can use listen for the port variable.

0
Jun 14 '18 at 13:01
source share

I think the best way is to use the dotenv package and set the port in the .env configuration file without changing the www file inside the bin folder.

Just install the package using the command:

 npm install dotenv 

requires this in your application:

 require('dotenv').config() 

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

 PORT=5000 

what is it.

More info here

0
Jun 13 '19 at 16:29
source share

If you are using Nodemon, I assume that the PORT 3000 is installed in nodemonConfig. Check if this is so.

0
Jul 19 '19 at 14:19
source share



All Articles