req.body returning undefined when I use consign in the application

1

I am familiarizing with the expression, so I created a simple Form with two fields, I have a JS file where I get the data from the form and send it through xmlHttpRequest and on the server side, I'm using consign to manage my routes, problem, is that whenever I add it, the return of the method that receives the form post on the server side always returns undefined.

form

<form id="form">
  <input type="text" name="name" id="name" id='form'>
  <input type="password" name="password" id="password">
  <button type="submit">Salvar</button>
</form>

app.js (Where I captured the form)

document.querySelector('#form').addEventListener('submit', (event) => {
  event.preventDefault();

  const name = document.querySelector('#name');
  const password = document.querySelector('#password');

  const person = {
    name: name.value,
    password: password.value,
  };

  console.log(person); //imprime o que eu quero normalmente

  const xhr = new XMLHttpRequest();
  xhr.open('POST', '/person', true);
  xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  xhr.send(JSON.stringify(person));
});

index.js (where I set up express)

let express = require('express');
let bodyParser = require('body-parser');
let consign = require('consign');

let app = express();

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.use(express.static('public'));

consign().include('routes').into(app);

app.listen(3000, () => {
  console.log('rodando');
});

In that file above, if I take out let consign , the line where I give an include in it and add an app.post to the route / users I can receive the data normally.

This is the file that is inside of the routes and is returning undefined:

module.exports = (app) => {
  const person = app.route('/person');

  person.post((req, res) => {
    console.log(req.body.value);
  });
}
    
asked by anonymous 24.12.2018 / 01:57

0 answers