Node - console.log (req.body) -undefined

1

Hi friends, I am trying to print a reply by console, that receives my app in node and that I am sending from Postman , but when I print in console it gives me the object undefined .

  

Server Started.

asked by E.Rawrdríguez.Ophanim 02.02.2018 в 19:46
source

1 answer

2

Use the following code, First you have to install

  

body-parser

Install it like this in your working directory:

  

npm install --save body-parser

The code already adapts it to use the library to parse the body of forms and bodys of json type.

I hope it serves you.

var express = require('express');
var engine = require('ejs-locals');
var bodyParser = require('body-parser');// instalar: npm install body-parser

var app = express();
//var path    = require("path");

//Parsear el body usando body parser
app.use(bodyParser.json()); // body en formato json
app.use(bodyParser.urlencoded({ extended: false })); //body formulario

var serv = require('http').Server(app);
const port = 3000;

   //engine & configuration
   app.engine('ejs', engine);
   app.set('view engine', 'ejs');


app.get('/', function(req, res) {
  res.render('home/index');
});

app.post('/test/product', function(req, res) {
  console.log(req.body);
  res.status(200).send({message:'Producto se ha recibido'});
});



serv.listen(port);
console.log('Server Started. ___ in port:' + port);
    
answered by 02.02.2018 / 20:18
source