Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
POST http://localhost:3001/createPost 404 (Not Found) EXPRESS & REACT

I'm trying to pass data through Axios, from a page created with React, to a Mongo database managed by an Express server. Saving to database works for users, but for posts I get these error messages:

POST http: // localhost: 3001 / createPost 404 (Not Found)

Uncaught (in promise) Error: Request failed with status code 404 at createError (createError.js:16) at settle (settle.js:17) at XMLHttpRequest.onloadend (xhr.js:66)

Can anyone help me out? I will attach the code below.

App.jsx

function App(){
  return (
      <Router>
        <Route path="/" exact>
          <Home />
        </Route>

        <Route path="/new">
          <NewPostFromHome />
        </Route>
    </Router>
  );
}

index.js (server)

const app = express();
app.use(cors());
app.use(express.json());

//Database connection
mongoose.connect("mongodb://localhost:27017/codingWaifus", {useNewUrlParser: true}, function(err, db){
  if(err){
    console.log(err);
  }
  else{
    console.log("Connected to "+mongoose.connection.name+" on Port: "+mongoose.connection.port);
  }
});
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));

app.use("/", require("./routes/userRoute"));
app.use("/new", require("./routes/postRoute"));

app.listen(3001, () => {
  console.log("running on port 3001");
});

postModel.js

const mongoose = require("mongoose");

const postsSchema = {
  title: String,
  body: String
}

const Post = mongoose.model("Post", postsSchema);

module.exports = Post;

postRoute.js

const express = require("express");
const router = express.Router();
const Post = require("../models/postModel");

router.route("/createPost").post((req, res) => {
  const title=req.body.title;
  const body=req.body.body;
  const newPost = new Post({
    title,
    body
  });
  newPost.save();
})

module.exports = router;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

it seems that the problem that you have is that the postRoute is under the

app.use("/new", require("./routes/postRoute"));

Here you can see that you used the "/new" route so the final Url would be

http: // localhost: 3001 / new / createPost

if you use this Url it should work properly

or you could also remove the "new" word to only keep this:

app.use("/", require("./routes/postRoute"));

for more details about this please check this documentation of middleware in express: https://expressjs.com/en/guide/using-middleware.html

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda