3

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.

Ewan Delanoy
  • 1,276
  • 2
  • 13
  • 31

3 Answers3

5

process.env variables are set by the "Environment". Basically, they act as access to your systems environment variables. When trying to access process.env.PORT it'll return undefined if you've not setup that environment variable in the shell that you're trying to run your system.

You can set the environment variable up before you run node app.js with the following.

$ PORT=8080
$ node app.js

In this case we set the environment variable within the existing shell, then we call node with the app.js file. Environment variables are passed from parent processes into processes they start, if you run those two in sequence, you'll be setting up an environment variable within the current shell, and node will then receive that when it starts up (along side all other environment variables).

To see all environment variables avalible to the existing node process you can run console.log(process.env);.

This will be the same for process.env.IP as well.

Side note: C9 and other similar environments often have a lot of pre-set environment variables. This is why it was available on C9. The same is true for Heroku as well, this is because their system must dictate the port your service should use so that their load balancers / reverse proxies can be pre-configured for that port.

Elliot Blackburn
  • 3,759
  • 1
  • 23
  • 42
2

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, ...)
R.A.Lang
  • 61
  • 2
1

After deploying your node project files, you'll need to make sure any env variables are also transferred.

1. Manually copy-paste env variables

From the original host config to the new host config. Use the dashboards on either end. Simple and secure.

--- or ---

2. Manually transfer .env file from project root

(Not recommended for production environments) Node projects often have a .env file in the root, which is commonly excluded from file transfers (for obvious security reasons). You'd need to make sure the new host also has the identical .env file in root. For production environments, this is usually not desired for security reasons, and it's better to use option #1.

Projects using this method are likely using the dotenv package which detects .env and makes the parameters available to your app. It's mainly for development convenience.

Kalnode
  • 9,386
  • 3
  • 34
  • 62