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);
port
is required by.listen()
, so you shouldn't try going without. 2) Are you getting any errors when running the application? Or does it just seem inaccessible from a browser? 3) Are you trying to access the website on the same machine withlocalhost:3000
,localhost:3001
, etc.? If you're using two machines, one client and one server, you'll need to add exceptions to the firewall on the server to allow Node to receive requests from the client. – Jonathan Lonowski.listen()
. Above when I say, "the app runs", this is the same as you saying, "no errors when running the application". When I say, "web page does not load", this is the same as you saying, "inaccessible from a browser". All access from the same machine (my server). Thanks for the feedback. – Benjamin Martin$ supervisor app.js
or$ PORT=[PORT] node app.js
when I want to set the environment port variable. I'll put up a code sample. – Benjamin Martin