App.post is not being executed ,req.body also

form.html:

DOCUMENT
</head>
<body>
    <form action="/greet" method="POST">
        <input name="person">
        <input>
        <button type="submit">submit</button>

    </form>
</body>

</html>

form.js:

const express=require(‘express’)
const app=express()

app.get(’/’,(req,res)=>{
app.send(“hello world”)

})
app.get(’/greet’,(req,res)=>{
let peson=‘Guest’

if(req.query.person){
    person=req.query.person
    res.send("Good Morning "+person)
}

})

app.post(’/greet’,(req,res)=>{
let peson=‘Guest’

console.log(req.body)

if(req.query.person){
    person=req.query.person
    res.send("Good Morning "+person)
}

})

app.get(’/form’,(req,res)=>{
res.sendFile(__dirname+’/file/form.html’)
})

app.listen(4444,()=>{
console.log(‘server started on http://localhost:4444’)
})

@sharma.deepti182 put a console.log(“Check”) to check if this code of block in executed at all, if not replace your

<button type="submit"> Submit </button>

with

<input type="submit" value="Submit"> 

and then try again with the console.log

I’ve made the changes still its not working

that block of code under app.post is not working

@sharma.deepti182 there are some change is post method, first in post request you have access to data as req.body not as req.query (hence for loops fails in app.post)

const express = require('express'),
  app = express()
;

// change here
app.use(express.json())
app.use(express.urlencoded({ extended: true}))

app.get('/', (req, res) => {
  res.send('hello world')
})
  
app.get('/greet', (req, res) => {
  let peson='Guest'

  if(req.query.person){
    person = req.query.person
    res.send('Good Morning ' + person)
  }
})
  
app.post('/greet', (req, res) => {
  let peson='Guest'
  console.log('check')
  console.log(req.body)
  
  // change here
  if(req.body.person){
    person = req.body.person
    res.send('Good Morning ' + person)
  }
})
  
app.get('/form', (req, res) => {
  res.sendFile(__dirname + '/file/form.html')
})
  
app.listen(4444,() => {
  console.log('server started on http://localhost:4444')
})

second is the use of express.json() and express.urlencoded({extended:true}), these two play a major part in POST and PUT requests.

a. express.json() is a method inbuilt in express to recognize the incoming Request Object as a JSON Object . This method is called as a middleware in your application using the code: app.use(express.json());

b. express.urlencoded({extended:true}) is a method inbuilt in express to recognize the incoming Request Object as strings or arrays . This method is called as a middleware in your application using the code: app.use(express.urlencoded({extended:true}));