Home > Blockchain >  node server cannot find the data in the body
node server cannot find the data in the body

Time:01-30

i am send a post request from fetch but the nodejs server is not able to access the body from the post request. I tried req.body but it just returns empty brackets.

const express = require('express');
const bodyParser=require("body-parser");


const app = express();
app.use(bodyParser.urlencoded({ extended: true}))
app.use(bodyParser.json());
app.use(express.json());

app.get("/user",function(req,res){
    
    res.send("just Checking");
    
});




app.post("/user", (req, res) => {
    console.log(req.body);
    res.send("got the data");

});
    




app.listen(3000,function(res){

    console.log("server is workig a t local host 3000");

    
    });
    

This is the browser side code



const checking = { name:"badmintion" }
function yo (){ 
     fetch("/user",{
method : "POST",
Headers : {
'Content-Type':'application/json'

},
body: JSON.stringify(checking)
})
}


yo();

In the browser i can see that the data is being sent but i am unable to recieve the in the nodejs server it just shows empty brackets.

CodePudding user response:

Remove body parser completely and try again. This tutorial mabe help u:

enter image description here

  • Related