Logan
Logan

Reputation: 31

Pug Form POST Body Empty When Submitting

I have a pug file with a form that send a POST request to a NodeJS endpoint, the endpoint will receive the request, but not the values in body.

pug form

form(action='/user_metricPOST', method='POST')
      input(type='text', name='user')
      input(type='text', name='start')
      input(type='text', name='end')
      input(type='submit', value='Search')

html source

<form action="/user_metricPOST" method="POST">
  <input type="text" name="user">
  <input type="text" name="start">
  <input type="text" name="end">
  <input type="submit" value="Search">
</form>

nodejs POST

server.post('/user_metricPOST', function (req, res) {
    console.log(req.body);
});

output

{}

Any help would be appreciated, thanks in advance!

Upvotes: 1

Views: 1574

Answers (1)

Logan
Logan

Reputation: 31

Thanks to @SebastianKaczmarek I figured out that adding this to my code fixed the problem!

const bodyParser = require('body-parser');
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());

Upvotes: 2

Related Questions