process.env.PORT is undefined (in LinuxCloud environment)

I’ve been developing a nodejs app on C9 for some time and now I’m trying to make a copy of it on my remote host. So far, in the new environment node app.js works in the console but I am unable to view the website in my browser.

It seems that it is a port issue. My app.js file goes like this :

var express     = require("express"),
    app         = express();
(...)
app.listen(process.env.PORT, process.env.IP, function(){
   console.log(process.env.PORT);
   console.log("The YelpCamp Server Has Started!");
});

In the C9 environment, the log tells me that process.env.PORT is 8080. But in the new environment, the log tells me that process.env.PORT is undefined.

How can I fix this ?

This is similar to this older question except that my remote OS is Linux not Windows. The answer to this question says that one should “modify the web.config file”, but I couldn’t find it on my remote host and I’m not sure it works the same way under Linux and under Windows.

Answer

I ran into something similar and ended up providing a default port number if one couldn’t be read from the environment.

const PORT = process.env.PORT || 8080;

app.listen(PORT, ...)

Leave a Reply

Your email address will not be published. Required fields are marked *