0

I have a following issue where I'm sending requests from my React frontend to my Express backend, using nginx as a proxy - and while the request gets sent to the backend, it remains pending forever (504 error code) until it times out (probably as no response gets back).

I'm not sure if the way I'm doing it is correct, I'm happy to get any help possible.

One important thing to mention is that is I remove the try {} catch {} block, and send back a response like res.send("All is good.") - then everything works.

If I include the try {} catch {} block, it never prints the response variable.

EXPRESS BACKEND:

const express = require('express');
const axios = require('axios');
require('dotenv').config();

const app = express();
const port = process.env.PORT || 6000;

app.use(express.json());
app.use(express.urlencoded({
  extended: true
}));

app.post('/api/add_customer', async (req, res) => {

  let shopifyUrl = 'https://' + process.env.SHOPIFY_URL + '/admin/api/2021-07/customers.json';

  try {
    const response = await axios.post(shopifyUrl, req.body,
    {
      headers: {
        'X-Shopify-Access-Token': process.env.SHOPIFY_PASS,
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      }
    })

    console.log('RESPONSE: ', response);

    res.send("All good");

  } catch (err) {
    res.send(err);
  }

});

app.listen(port, () => console.log(`Listening on port ${port}`));

nginx config:

server {
        listen 80;

        server_name VPN_IP_ADDRESS;

        location / {
                root   /var/www/html/;
                index index.html;
                try_files $uri /index.html$is_args$args =404;
        }

        location /api {
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-NginX-Proxy true;
                        proxy_pass http://localhost:6000;
                        proxy_ssl_session_reuse off;
                        proxy_cache_bypass $http_upgrade;
                        proxy_redirect off;
        }
}
  • 1
    You can hit the API with postman ? – edhi.uchiha Jul 28 '21 at 01:53
  • The 504 originates from the Shopify API, which is a gateway timeout - a really unhelpful error that just means it timed out for some reason. The best way to attack this is to manually test this with postman, and better yet with an integration test. Have a look at this post [https://stackoverflow.com/questions/38374334/getting-504-gateway-timeout-nodejs](https://stackoverflow.com/questions/38374334/getting-504-gateway-timeout-nodejs) and I would also use this wrapper too [https://github.com/MONEI/Shopify-api-node](https://github.com/MONEI/Shopify-api-node) – Nick Mitchell Jul 28 '21 at 06:38
  • @NickMitchell I have tried the wrapper, but same thing is happening, it's just pending for ever until it times out. – jason12300 Jul 28 '21 at 08:48
  • @edhi.uchiha Nope, I cannot. Would that be an issue with my nginx config? – jason12300 Jul 28 '21 at 08:54
  • OK so a few clean ups. First, drop the nginx bit for now and run express standalone eg. `node app.js` or whatever the express server file is called. The second is that axios throws an error that has error.response see [https://axios-http.com/docs/handling_errors](https://axios-http.com/docs/handling_errors) so change the catch block to `if(err.response){ const {status, data} = err.response; res.status(status).json(response); }`. Finally, let's solve the 504 by digging into these solutions from [this comment](https://stackoverflow.com/questions/38374334/getting-504-gateway-timeout-nodejs) – Nick Mitchell Jul 29 '21 at 08:56

0 Answers0