express post tutorial to send request body code example

Example 1: express js post request body

const express = require('express')

const app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.post('/profile', function (req, res, next) {
  console.log(req.body)
  res.json(req.body)
})

Example 2: express receive post data

const { id } = req.body;

Example 3: app.post (req res) get data

const express=require('express');
const app=express();
//accept Post with no midleWare express
app.post("/register/",function(req,res){
    var bodyStr = '';
    req.on("data",function(chunk){
        bodyStr += chunk.toString();
    });
    req.on("end",function(){
        res.send(bodyStr);
    });

});