Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  • 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.

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)).

I found this on line 220 of /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.

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

Thanks!

Edit: Code Sample (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);
like image 579
Benjamin Martin Avatar asked Aug 02 '13 04:08

Benjamin Martin


People also ask

Why is port 3000 used when running a NodeJS application?

3000 is a somewhat arbitrary port number chosen because it allows you to experiment with express without root access (elevated privilege). Ports 80 and 443 are the default HTTP and HTTPS ports but they require elevated privilege in most environments.

Which port number is used to execute Expressjs programs?

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

How do I run NodeJS on different ports?

If you want to run NodeJS on different port, change 80 to your required port number (e.g 8080). Also the function defined in createServer() will be executed whenever someone sends a request to your NodeJS server.


3 Answers

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'));
like image 70
EhevuTov Avatar answered Oct 22 '22 19:10

EhevuTov


In bin/www, there is a line:

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

Try to modify it.

like image 38
Larry Lu Avatar answered Oct 22 '22 18:10

Larry Lu


Try this

$ PORT=8080 node app.js
like image 14
John Williams Avatar answered Oct 22 '22 17:10

John Williams