3

I have the following form and submitting it results in an empty req.body This is my HTML:

<form action="/contact" name="contactUs" id="contactUs" method="POST">
   <label for="email">Email Message</label>
   <input type="text" name="email" id="email">
  <input type="submit" value='Save'>
</form>

And this is (part) of my server.js:

 app.use(bodyParser.json());
 app.use("/", expressStaticGzip("dist"));

app.post("/contact", (req, res) => {
  console.log('anyone there?')
  res.json(req.body);
});

The console.log works fine. I get an empty object for req.body though.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
harrydry
  • 107
  • 2
  • 11

1 Answers1

13

You're submitting as HTML POST that means you are using Content-Type: application/x-www-form-urlencoded.

This means that you need to add app.use(express.urlencoded({ extended: false })) , This way express can parse that request and put it to req.body

Chanlito
  • 2,322
  • 2
  • 17
  • 27