REST Api - Express, ReactJS, React-Router-Dom, NGINX (Request failed with status code 405)

Viewed 37

I am trying to create a REST Api using Express on NodeJS as the backend and ReactJS as the frontend.
I am using Nginx as the server.

Here comes the issue.
Everything works completely fine when I test on my local machine. No issues there.
But the second I move it over to my server, build my react app and run my express node environment and test it via the domain, it falls to pieces.

Running the post request on my live server keeps throwing me this error:

XHRPOSThttps://example.com/api/someRequest
[HTTP/3 405 Method Not Allowed 53ms]

Uncaught (in promise) Error: Request failed with status code 405

This is how I do my post requests frontend:

import { post } from "@import/blahblah";
let response = await post("/someRequest");

My @import/blahblah:

import axios from "axios";

const isLocalHost =
  window.location.hostname === "localhost" ||
  window.location.hostname === "127.0.0.1";

export async function post(url, args = {}) {
  const Axios = axios.create({
    withCredentials: true,
    baseURL: isLocalHost
      ? "http://localhost:5000/api"
      : "https://example.com/api",
  });

  let response = await Axios.post(API_URL + url, args);
  return response.data;
}

This is what my backend looks like:

const express = require('express')
const cors = require('cors')
const http = require('http')
const cookieParser = require('cookie-parser')

require('dotenv').config()

// Create server connection
const app = express()
const server = http.createServer(app)

// Set app uses
app.use(express.json())
app.use(cookieParser())
app.use(express.urlencoded({ extended: true }))

app.use(
  cors({
    credentials: true,
    origin: process.platform === 'win32' ? 'http://localhost:3000' : 'https://example.com'
  })
)

server.listen(5000, () => console.log('Server - Started (port 5000)'))

  app.post('/api/someRequest', async (req, res) => {
    res.send(invite)
  })

Would really appreciate any help that I could get.

1 Answers

Figured out that my Nginx config wasn't configured for the API.
So what I did was add the API location to my config.

Related