Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

456
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

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 
            }
        )
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!