简体   繁体   中英

Passing Angular variable to Express backend through POST

req.body is always empty. I'm not sure what I'm doing wrong? I tried adding content-type headers as json but that didn't do anything either. Can someone lead me in the correct direction please? Thank you

EDIT: just for clarification purposes, my Angular frontend hits the backend function successfully, but req.body is empty. If I understand everything correctly, if I'm using the 'body-parser' library, it should be passed in through post through 'req.body'. I'm just not seeing that though and I'm not sure why.

EDIT2: I have the body parser code in my app.js but the backend routing in a index.js file, does that have anything to do with it?

EDIT3: app.js http://pastebin.com/9vNgf0Nd index.js http://pastebin.com/icLa3e2X

ANGULAR FRONTEND

service.registerAccount = function(account) {
        console.log(account);  //account = { userName: 'test', password: 'hello' }
        return $http({
            method: 'POST',
            url: '/register',
            data: { account: account },
            headers: {'Content-Type': 'application/json'}
        });
    }

BACKEND (app.js)

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

BACKEND (index.js)

var express = require('express');
var router = express.Router();    

router.post('/register', function(req, res) {
  console.log(req.body);
};

Please remove this line

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

Also,

app.use(bodyParser.json());

have to be called before app.use('/', routes);

And make sure to add Content-Type: application/json to the request header

What happens if you add the content type?

service.registerAccount = function(account) {
        console.log(account);  //account = { userName: 'test', password: 'hello' }
        return $http({
            method: 'POST',
            url: '/register',
            data: { account: account },
            headers: {
               'Content-Type': 'application/json'
            }
        });
    }

There is nothing wrong in the UI code. Not sure what is router so you may try this or post the code for router.

Try this (this works for me) or you can also use your router:

app.post('/register', function (req, res) {
  console.log(req.body);
  res.send('welcome, ' + req.body.account.username)

});

You are missing:

var app = express();
app.use(router);

If you want to user routers refers to following example:

UPDATE with full code:

var express = require('express');
var router = express.Router();    
var app = express();
app.use(router);
router.post('/register', function(req, res) {
  console.log(req.body);
};
app.route('/register')
  .post(function (req, res) {
    console.log(req.body);
    res.send('welcome, ' + req.body.account.username)
  })

Try this

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

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

app.post('/register', function(req, res) {
  console.log(req.body);
});

var server = app.listen(8081, function () {

   var host = server.address().address
   var port = server.address().port

   console.log("Example app listening at http://%s:%s", host, port)
});

and then execute this from prompt:

$ curl localhost:8081/register -v --data "{\"name\":\"test\",\"password\":\"hello\"}" --header "Content-Type: application/json"

this works for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM