angularjs - In Express.js with body-parser the value of request.body is undefined -


i'm having problem cannot diagnose.

on server, have simple url handler using express.js:

var express = require('express'); var app = express();  var bodyparser = require('body-parser'); var multer = require('multer');  app.configure(function() {     app.use(app.router);     app.use(bodyparser.json()); // see: http://expressjs.com/api.html#req.body     app.use(bodyparser.urlencoded({         extended: true     })); });  app.post('/submit', function (req, res) {     console.log(req.body); }); 

on client side, there's form handled angular controller:

$scope.submit = function () {     // $http.post('/submit', $scope.data); // post request send data server     $http({         method: 'post',         url: '/submit',         data: $scope.data     });      console.log('post /submit ' + json.stringify($scope.data)); }; 

in browser's console fine: $scope.data valid; node.js responds console.log, expected, writes undefined means that, well, request.body undefined.

what do wrong? how can fix it?

if you're using express 3 shouldn't have use body-parser module bundled express 3 express.bodyparser. you're getting empty body because you're putting app.use(app.router) before body parser.

app.configure(function() {     app.use(express.bodyparser());     app.use(app.router); }); 

which why other solution working:

app.post('/submit', bodyparser.json(), function (req, res) { 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -