0
votes

Im using aws cloud9 for dev and codestar for deployment. The service is working perfectly locally and after deployment there is one post method giving me the 502 error.

The method from server.js:

app.post('/upload', async function(req, res) {
  const url = "https://m.media-amazon.com/images/M/MV5BMjIxMjgxNTk0MF5BMl5BanBnXkFtZTgwNjIyOTg2MDE@._V1_UX182_CR0,0,182,268_AL_.jpg";
  const id = "tt0993846";
  const stream = request(url).pipe(fs.createWriteStream(`images/${id}.jpg`));

  const putObjectWrapper = (params) => {
    return new Promise((resolve, reject) => {
      const s3 = new AWS.S3({ params: { Bucket: 'my-upload-bucket-idan', Key: id+'.jpg' } })
      s3.putObject(params, function (err, result) {
        if(err) reject(err);
        if(result) resolve(result);
      });
    })
  }

  const store = async function() {
    try {
      const data_stream = fs.createReadStream(`images/${id}.jpg`);
      const s3 = new AWS.S3({ params: { Bucket: 'my-upload-bucket-idan', Key: id+'.jpg' } });
      const request = s3.putObject({Body: data_stream});
      const result = await request.promise();
      handleSuccess(result.ETag, res);
    } catch (err) {
      console.log(err);
      handleError(err, res);
    }
  }
  
  stream.on('close', () => {
  store();
});
});

the error:

<html>
<head>
    <title>502 Bad Gateway</title>
</head>

<body>
    <center>
        <h1>502 Bad Gateway</h1>
    </center>
    <hr>
    <center>nginx/1.16.1</center>
</body>

</html>
1
Seems like a port issue. Your application may run on different port that nginx is forwarding requests to.Marcin
But all of my other methods work. Its just this one that returns the 502.Idan Brinza

1 Answers

0
votes

I fixed it. The problem was I didn't have an "images" folder on my server. Don't know why it gave me a 502 though.