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

221
Visualizações
Post request from fetch gives a 404 server error (javascript/express)

I'm trying to see if a post request would work on my server at the '/db' route. The get request works just fine, but the 'Post' request gives a '404' not found error.

(Part of) my js code:

function getReq() {
    let p = fetch("http://localhost:3000/db").then((a)=>a.json()).then((x)=>console.log(x))
}

    async function postReq(obj) {
        console.log(obj)
        const p = await fetch("http://localhost:3000/db",
            {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: obj
            }
        )
    }

My express code:

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

app.get('/db', (req,res)=>
{
    res.sendFile("db.json");
});

app.post("/db", function(req,res) 
{
    res.send("ok");
});

app.use(express.static('public'));
app.listen(3000, (ok) => console.log("Okk"));

db.json and app.js are in the same folder, and I only call postReq with json strings. The rest of the files are in the 'public' directory. Could anyone guide me to what I could be doing wrong? Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to stringify the body because your content-type is application/json

async function postReq(obj) {
        console.log(obj)
        const p = await fetch("http://localhost:3000/db",
            {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(obj) // Here 
            }
        )
    }
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