I have running in an Amazon Web Server a ReactJS Frontend connecting to an Express APIREST. With PM2 to launch both processes and NGINX.
The problem comes when I try to log-in to the app in chrome, the frontend is okay but it refuses to connect to the APIREST.
Error Image
These are the files I have:
APIREST .env
DB_CCT=my_db
DB_CCT_USER=user
DB_CCT_PASS=password
DB_CCT_HOST=127.0.0.1
NODE_ENV=development
PORT=8017
JWT_SECRET=jotauvedoblete
FRONTEND ReactJS .env
I also tried to add the public IP instead of localhost or 127.0.0.1 but doesn’t work either, it shows the connection timed out.
REACT_APP_DB=http://localhost:8017/api/v1/
PM2 ecosystem.config.js
module.exports = {
apps: [{
name: 'CCT_REACT',
cwd: 'cct-web-client/',
script: 'npm',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'run start:production',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 5001
},
env_production: {
NODE_ENV: 'production',
PORT: 5001
}
},
{
name: 'CCT_API_REST',
cwd: 'cct_api_rest/',
script: 'npm',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'run start',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 8017
},
env_production: {
NODE_ENV: 'production',
PORT: 8017
}
}],
deploy: {
production: {
}
}
}
NGINX service
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/CCT/cct-web-client/build;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri/ /index.html?q=$uri&$args;
}
}
2
Answers
If you are getting a connection refused, which means that the requests are getting blocked.
I see that you have specified PORT 8017 for your API App, check 2 things –
Allow connect to port 80 on your aws and remove all other and use following nginx config. Also set api backend url in your frontend to your domain or ip on port 80.
}
Also make sure to check first with any api client if you can consume your api without frontend. It should not be timedout.