Home > Back-end >  Axios 404 error, two different localhosts
Axios 404 error, two different localhosts

Time:10-30

I'm running the server on localhost:3000 and my React app is on localhost:8080. I've looked at other Stack Overflow questions and it seems setting the full url worked but it's not in my case.

sever.js

const sendAxie = require("./sendAxie");

const express = require("express");
const path = require("path");
const cors = require("cors");

const app = express();
const publicPath = path.join(__dirname, "..", "public");

app.use(cors());
app.use(express.static(publicPath));

app.post("/send-axie", (req, res) => {
  // console.log(req.body);
  console.log("in send-axie");
});

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
  console.log("Server is up on "   PORT);
});

app.js

axios
  .put("http://localhost:3000/send-axie", { axieID })
  .then((response) => {
    console.log(response);
  })
  .catch((err) => {
    console.log("in catch");
    console.log(err);
  });

Console:

PUT http://localhost:3000/send-axie 404 (Not Found)
Error: Request failed with status code 404
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.onloadend (xhr.js:66)

CodePudding user response:

as I understand it, you make a PUT request, but in your routes only POST. You need to add this

app.put("/send-axie", (req, res) => {
  // console.log(req.body);
  console.log("in send-axie");
});

CodePudding user response:

In your express app, you have wrote a post api. In react you are calling a put api. Change that to post

  • Related