Empty req.body when doing http.post

@totalcommnz Totalcommnz
Sun, 10 Jul 2016 06:52:25 +0000
ionic 2

@totalcommnz wrote:

Hi,

I am using ionic2 and I am trying to do an http.post request to my nodejs express REST API. I can see the request is received, but I cannot get the request parameters. I double-checked I am using bodyParser, I checked req.body, req,query, req.params, but they are all empty. Any clue where I should look?

My code:
Ionic2:

var body = JSON.stringify({
    name: "My name",
    email: "myemail@email.com",
    id: "1092379817239821"
});
return this.http.post(URL, body).map(res => res.json());

Express server.js:

var routes = require('./modules/routes');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/', routes);

routes.js:
var express = require('express');
var router = express.Router();
router.post('/create-user', function(req, res) {

var name = req.body.name,
      email = req.body.email,
      id = req.body.id;

    //they are all empty

});

Posts: 2

Participants: 2