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.
The pm2 list
is above and id 8
have port number 3001
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) name:
for each processenv:
in the PM2 configuration make sure they are correctly set to 3000
and 3001
respectively.3000
.ie 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...
pm2 stop all
sudo apt-get install nginx
sudo nano /etc/nginx/conf.d/default
Or whatever editor you want
server { listen 3001; listen [::]:3001; location / { return 200 '3001 works!'; } }
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.
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;
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.