1
votes

I uploaded the following file named server.js:

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

app.get('/', function (req, res) {
    console.log('Send message on get request');
    res.send('Hello full-stack development!');
});

app.set('port', process.env.port || 3000);

var server = app.listen(app.get('port'), function () {
    console.log('Express server listening on port:' + 
    server.address().port);
}); 

along with the following package.json file:

{
    "name": "newswatcher",
    "version": "0.0.0",
    "description": "NewsWatcher",
    "main": "server.js",
    "author": {
        "name": "myname",
        "email": "myemail"
    },
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "express": "4.17.1"
    }
}

and the package-lock.json and the npm modules. I followed the basic steps to create an aws application and then upload and deploy the zip file with the four files I have mentioned in and have set the environment variable NPM_CONFIG_PRODUCTION to true. Then when I go to the environment I get the 502 bad gateway error.

It works fine when I run it locally with npm start.

This is from the logs which make it seems like it is working:


/var/log/web.stdout.log


Jan 10 01:46:27 ip-172-31-13-23 web: > [email protected] start /var/app/current

Jan 10 01:46:27 ip-172-31-13-23 web: > node server.js

Jan 10 01:46:27 ip-172-31-13-23 web: Express server listening on port:3000

Jan 10 01:49:59 ip-172-31-13-23 web: > [email protected] start /var/app/current

Jan 10 01:49:59 ip-172-31-13-23 web: > node app.js

Jan 10 01:49:59 ip-172-31-13-23 web: Server running at http://127.0.0.1:8080/

Jan 10 01:58:11 ip-172-31-13-23 web: > [email protected] start /var/app/current

Jan 10 01:58:11 ip-172-31-13-23 web: > node server.js

Jan 10 01:58:11 ip-172-31-13-23 web: Express server listening on port:3000

This is from the error logs:

2021/01/10 02:24:23 [error] 4619#0: *431 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.24.137, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.13.23"

2021/01/10 02:24:23 [error] 4619#0: *433 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.10.27, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.13.23"

These go on and on like this.

What is wrong?

1
Your logs show that express is on port 3000, but nginx expects 8080.Marcin
thanks, i thought aws would automatically make them in sync. I have the app port set to process.env.port and 3000 if that is not set. Was process.env.port not defined then?Michael93
No,its up to you to set it up. Did you manage to change your ports and did it work?Marcin
Yes I changed the port in server.js to 3000 and redid everythingMichael93
Glad to hear that. If you don't mind I will provide an answer for future reference.Marcin

1 Answers

0
votes

Based on the comments.

The issue was caused by the fact that the express was litenting on port 3000, but nginx expected port 8080.The solution was to use port 3000 for nginx.