3

We are using 4 different express projects in a single server, running pm2 start cmd from each project root directory to start the app.

And using ec2 Linux server, added port 3000, 3001 to inbound rules, both runs fine but not able to access API's through 3001, and able access by 3000, what could be an Issue?

the same project running at 3000, but not in 3001, so we can confirm, there is no bug in our projects.

Tried at terminal nodemon app it runs fine but not accessed by Postman getting same error :

Could not get any response

There was an error connecting to http://ec2-x-x-amazonaws.com:3001/api/login.

how can we run multiple node/express projects in available/defined ports?

3001    tcp 0.0.0.0/0, ::/0 ✔
3000    tcp 0.0.0.0/0, ::/0 ✔

Some ports running without add as an inbound rule like 8088, 8089.

pm2 status shows all ports are running fine.

enter image description here

The pm2 list is above and id 8 have port number 3001

151291
  • 3,308
  • 7
  • 48
  • 81
  • in that case mostly check with `pm2 status` if the app is running which is expected to run in `3000`, and also check if its listning in `3000` only not any other port. – Saikat Chakrabortty Jan 23 '19 at 05:36
  • @saikatchakrabortty - port `3000` running fine, but `3001` does not run from `pm2 status`, how can I archive it? – 151291 Jan 23 '19 at 08:35
  • @saikatchakrabortty - restarted all ports `pm2 restart all --watch`, now `3001` also running fine, but not able to access from postman, and did not get any error log from `pm2 logs` – 151291 Jan 23 '19 at 09:05
  • 1
    make sure once again, you have added inbound and outbound rules, – Saikat Chakrabortty Jan 23 '19 at 09:08
  • @saikatchakrabortty added `3000` and `3001` at inbound rules but what can we update in outbound rules? accepting all ports there. – 151291 Jan 23 '19 at 09:43
  • Through what host are you trying to access those ports? A public IP assigned to the instance or something else? – cantuket Jan 26 '19 at 05:04
  • @cantuket - Yes, and public IP assigned to those ports. – 151291 Jan 26 '19 at 06:14
  • To answer your previous question, yes, you should leave your out bound rules to ALL 0.0.0.0/0 unless you have a good reason not to – cantuket Jan 27 '19 at 01:25
  • Can you show us a screen shot of what `pm2 list` prints? – cantuket Jan 27 '19 at 01:56
  • @cantuket - updated `pm2 list` – 151291 Jan 28 '19 at 07:10
  • @151291 are you able to access the port 3001 from inside the server? Like `curl -I localhost:3001` or similar. If you can, there is some other issue with your configuration. – gargsms Jan 29 '19 at 06:53
  • @gargsms - I hit the above cmd inside server terminal, getting error response `HTTP/1.1 404 Not Found, X-Powered-By: Express` host is `ec2` instead of `localhost` because app running in the `ec2` hostname. – 151291 Jan 29 '19 at 11:28
  • That seems fine. I am not sure what the issue is then. Port 3001 seems not accessible from outside the server. You should look more into your EC2 configuration. I don't think pm2 has anything to do with it. – gargsms Jan 29 '19 at 16:43
  • How are you loading the app, the specific pm2 commands. Your status looks like it's the same app running. What happens if you try hitting the non-api routes at port 3001? – adamrights Jan 30 '19 at 05:47
  • Also, try `netstat -ntlp | grep 3001` on the ec2 instance command line – adamrights Jan 30 '19 at 05:53
  • @adamrights - Have 4 projects, all project has app.js I am running pm2 in each root directory. so not possible for non-api routes. and tried `netstat` cmd, getting good response `tcp 0 0 x.x.x.x:3001 0.0.0.0:* LISTEN 32702/node /home/ec2` – 151291 Jan 30 '19 at 06:14
  • So there is a node process listening on 3001. Are the other 2 apps also working? Are they explicit in the ports they use? Maybe one is stealing 3001? – adamrights Jan 30 '19 at 06:20
  • @adamrights - other ports are running and accessing from `postman` fine, all using different ports checked by `netstat -ntlp | grep node` – 151291 Jan 30 '19 at 06:32
  • Hmm. So I think we'd need to see that api code. Other thoughts, maybe if you don't hit that endpoint with certain headers, cookies, etc...its behavior is to 404. The x-Powerered-By: Express tells you the nodejs server handled that 404. Also, have you tried `https://` instead of `http://` – adamrights Jan 30 '19 at 06:50
  • @adamrights - other running ports also showing `404` from terminal, the server does not have ssl. I am using header `Content-Type : application/json` like other ports. – 151291 Jan 30 '19 at 06:56
  • 1
    This all looks like you just need to open port 3001 in your EC2 security group... – Dusan Bajic Feb 01 '19 at 07:49

3 Answers3

3

NOTE: Not an answer necessarily, but these are some things I've screwed up before and this is the process I would use to troubleshoot...

PM2 Configuration

First check this to make sure you don't have any conflicts.
(As mentioned in comments please provide screen shot of pm2 list and even the, PM2 config files if possible)

How are you running PM2, a single ecosystem.json config? Multiple ecosystem.json configs? Either way...

Make sure that each app has correctly specified properties...

  • script:(start-up script)
  • cwd: (working directory)
  • Unique name: for each process
  • If you're specifying your environment variables env: in the PM2 configuration make sure they are correctly set to 3000 and 3001 respectively.
  • Make sure you haven't accidentally hardcoded both app to run on 3000 .i.e make sure these env configs are actually being used.

Another thing to note here is that if you update your PM2 config files a simple pm2 restart won't look for the new configuration. You will need to add the --update-env flag.



Basic Port Web Server Test

If the above checks on PM2 are fine then I would start narrowing things down further by eliminate node entirely for now. You could try something like this for a quick test of the accessibility of port 3001...

  1. Stop all pm2 processes

pm2 stop all

  1. Install Nginx

sudo apt-get install nginx

  1. Edit default config file

sudo nano /etc/nginx/conf.d/default Or whatever editor you want

  1. Configure server. Add to the top of file...

 server {
      listen 3001;
      listen [::]:3001;
      location / {
          return 200 '3001 works!';
      }
  }
  1. Restart Nginx

sudo service nginx restart


Now try to access the public IP at port 3001 and it should download a text file to your machine with "3001 works!"

If this works the issues is not EC2 or the related security groups, but rather your node servers.

cantuket
  • 1,582
  • 10
  • 19
2

if your code looks like this

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var port = process.env.PORT || 3000;

app.listen(port, function () {
  console.log('Example app listening on port ' + port + '!');
});

you can change the port from the command that starts the server like so

PORT=8000 node index.js

for more exmaples check this out https://gist.github.com/indiesquidge/7fe1d8be1b973f782c97

You could also pass the port as an argument to your script using process.argv

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var port = getPort();

app.listen(port, function () {
  console.log('Example app listening on port ' + port + '!');
});

function getPort(){
    if(process.argv.length > 2){
        return process.argv[2];
    } else {
        return 3000;
    }
}
Haris Bouchlis
  • 2,366
  • 1
  • 20
  • 35
0

This is a port number issue, re-checked from server inbound rules, whether the port 3001 and 3002 etc added correctly with custom TCP rule and source must be anywhere. now runs fine.

ec2-Instances -> launch-wizard-1 -> right click -> edit inbound rules.
151291
  • 3,308
  • 7
  • 48
  • 81