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;
}
}