Problem with bodyParser - Does not take req.body even with bodyparser installed and configured

Asked

Viewed 101 times

-3

I’m trying to create my first REST MVC API with Nodejs and Express, only I can’t get the req.body information, bodyParser is configured. I’ve researched everything and I can’t find the problem, follow the github link of my project. I wanted a lot of help, please.

https://github.com/Thiagosousadev/posts

inserir a descrição da imagem aqui

  • Important you [Dit] your post and explain in detail the problem with a [mcve]. Studying the post available on this link can make a very positive difference in your use of the site: Stack Overflow Survival Guide in English

1 answer

-2


Good evening Thiago, congratulations for the initiative in the world Node.js,here’s a tip oara your career as DEV always put the middlewares that receive the data,before the routes:

    //BodyParser
    app.use(bodyParser.urlencoded({extended: true}))

    app.use(bodyParser.json())**

    // Configurações
    // Banco de Dados

    require('./database/database');

    //Engine e frontend
   app.set('view engine', 'ejs');
   app.set('views', __dirname + '/views')
   app.use(express.static('public'));
   //app.use(expressLayouts);
  • Thanks for answering, but I did what you recommended and unfortunately continues the same error. I use Postman to test the API. I’ll send you the prints from Stanley’s.

  • I appreciate if you send, it will be interesting to see what’s wrong....

  • It returns in the post route as an empty json: { }

  • Already tried to send the data using POST ? ,there the method this GET,the sending of data by default uses POST.

  • Yes I fixed and it worked, thank you very much for your help

  • I thank you, keep trying I saw your code and you program very well,and makes subdivision of the files in folders in professional way.

  • Thank you very much, actually I realized that the error was in my statico js file, I first made the simple front end and used innerHTML to delete the input fields when clicking the button, Every time I sent the form to the post route it erased everything that was in the inputs and therefore sent an empty object. Thank you for your help and for evaluating my code, it helps me a lot to improve.

  • It’s us!!!!! Long live DEV’s! !!!!!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.