Trying to enable a POST request through AJAX from a site to an express server triggered some unexpected challenges. When running the server on the same localhost port as the website (localhost:8080), the webpage fails to render and an error message pops up saying, "Cannot GET /mogle/income.php." This is happening despite the fact that "mogle/income.php" is intended to be the page for the request. Conversely, when changing the server to a different port (e.g., localhost:8000), the AJAX POST call stops functioning altogether. The common belief appears to be that requests across different ports can only be done with JSONP, which exclusively supports GET requests, while my requirement demands the use of POST.
In an attempt to rectify the issue with the webpage failing to render on the same port scenario, I implemented an app.get function in my server.js file, outlined below. However, this resulted in the PHP file being downloaded instead of rendering as expected, likely due to its file type difference compared to HTML files. As a result, there arises a concern about having to implement individual app.get statements for every other webpage present on the site (e.g., homepage.php, login.php) within the server.js file.
app.get("/mogle/income.php", (req, res) => {
// Using "../../" due to server file location
res.sendFile(path.join(__dirname, "../../income.php"));
});
app.post("/path", async (req, res) => {
// Handle response
});
Reflecting on the constraints and issues faced, it beckons the question - what is the standard practice for transmitting requests (both POST and GET) from websites to servers regarding port configurations?