ExpressJS behaves differently when API requested with and without PORT number

41 Views Asked by At

I have 2 flutter web apps, one is working perfectly fine when i request the url and the other works only when i do include the port xxxxx:4000/nexus-vote but when removing the port the status code 200 comes and i can see that the index.html is coming in the payload(checked with postman) but not really showing anything in the browser..

I thought it's something related to nginx, but the forwarding is happening fine as i see the request in the log of the JS app but have no idea what is the problem though...

Here's my code for the nginx :

    location = /nexus-vote/ {
    #proxy_set_header Host $http_host:4000;
    #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_set_header X-Forwarded-Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect https://$server_name/nexus-vote/ https://$server_name:4000/nexus-vote/;
    #proxy_redirect https://$server_name/ https://$server_name:4000/;
    proxy_pass https://localhost:4000/nexus-vote/;
    #proxy_redirect off;
}
  location /iframe/ {
    proxy_set_header Host $http_host:$server_port;
    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 https://localhost:3000/iframe/;
    proxy_redirect off;
}

and here's the express code served:

  app.get("/nexus-vote", async (req, res) => {
  try {
    console.log(`serving from js ${__dirname}`);
    res.status(200)
      .set("Content-Security-Policy", "default-src *; style-src 'self' http://* 'unsafe-inline'; script-src 'self' http://* 'unsafe-inline' 'unsafe-eval'")
      .sendFile(path.join(__dirname + "/web/index.html")); // "/privacy_and_terms.html"));
  } catch (e) {
    console.log(e);
  }
});
1

There are 1 best solutions below

0
Waged On

The problem was that in each flutter web app there's

that needed to be with the same name of the static js file :

app.use("/nexus-vote",express.static(path.resolve(__dirname + "/web")));

and i had 2 apps that was pointing to web that's why it was behaving in a strange way..