0

I've been trying to use body-parser but it's deprecated so I'm doing this

var express = require('express')
var app = express()

app.use(express.static(__dirname))
app.use(
  express.urlencoded({
  extended: true
  })
)
app.use(express.json())

app.post('/send-message', (req, res) => {
console.log('request', req.body);
res.sendStatus(200)
})

var server = app.listen(3000, () => {
  console.log('server is listening on port', server.address().port)
})

And send post request from Postman

enter image description here

But in the console I get

server is listening on port 3000
request {}

Why request is empty? and how to catch it correctly?

PHP User
  • 2,350
  • 6
  • 46
  • 87
  • 1
    Provide the `Content-Type: application/json` header when making your Postman request. (If I remember correctly, that "Text" dropdown has a "JSON" option that populates this header for you) – George Mar 09 '21 at 16:45
  • How did you try to use body-parser ? – Dilshan Mar 09 '21 at 16:56
  • Does it answer your question https://stackoverflow.com/questions/30126189/non-deprecated-alternative-to-body-parser-in-express-js ? Here are more references https://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4 – noob_nerd Mar 09 '21 at 17:06
  • thanks to you @George that worked – PHP User Mar 09 '21 at 19:28

1 Answers1

1

select json from the dropdown menu where text is written, this will set the content-type header to application/json text to json