Kevin Powell - CommunityKP-C
Kevin Powell - Community17mo ago
5 replies
vic

how to send post from data from react and capture the data in express

react code
const [formDetails,setFormDetails]=useState({
    name : '',
    mail:'',
    password:''
   });
    
async function HandleForm(e){
        e.preventDefault()
        console.log("inside fetch fn")
        try{
           let res=await fetch('http://localhost:4040/register',{
            method:'POST',
            body:formDetails,
            headers:{
                "Content-type":"application/json"
            }
           });
           console.log("inside try block")
        }
        catch(err){
            console.log(err)
        }

    }

<form onSubmit={HandleFrom}>
<input type="submit" />
</form>

express codE:
routes.post('/register',async(req,res)=>{
    const receivedData=req.body;
    console.log(receivedData)
}
Was this page helpful?