Search code examples
node.jspug

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!


Solution

  • 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());