I have vitejs(react) front-end application and a nodejs backend server which I want to use to query a mysql database. This works fine on my computer but on AWS I'm getting a 'failed to fetch' error. I'm not sure if this is caused by cloudfront, nginx, the nodejs server on port 3001, or making an http fetch request from a https website. I would like to know which page I need to fix. The node.js api I can access fine both from ip:3001/api and localhost:3001/api.
Fetch on App.tsx
useEffect(() => {
fetch("http://localhost:3001/api")
.then((res) => res.json())
.then((data) => setData(data.message))
.catch(rejected => {
console.log(rejected);
})
}, []);
the nodejs server port 3001 code
import express from "express";
import cors from "cors";
const app = express();
app.use(express.json());
app.use(cors())
const PORT = process.env.PORT || 3001;
app.get("/api", (req, res) => {
var allowedOrigins = ['https://example.com']
res.header('Access-Control-Allow-Origin', allowedOrigins)
res.json({
"message": "dynamic json",
"mytest": "server"
})
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
console.log(`Server running`);
});
nginx conf.d
server {
listen 80;
listen 443;
server_name _;
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:3001;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}