Unable to get $http.post request data in Node.js res.body
some ideas :
- Maybe an URL error ? Make sure you aren't using a prefix like
app.use('/api', router);
- Look at the
Content-Type
:
application/x-www-form-urlencoded --> 'var1="SomeValue"&var2='+SomeVariable
application/json;charset=UTF-8 --> {var1:"SomeValue", var2:SomeVariable}
- You could use
$http
more explicitly :
$http({ url: '...', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: 'var1="SomeValue"&var2='+SomeVariable});
My best guess is that your angular $http request URL is pointing to a bad end-point.
Angularjs
// data to post nodejs servervar _data = { 'message': 'Can I help you?'};// angularjs $http post request$http.post('/api/url', _data).then(function(respond){ // if success console.log(respond);}, function(error){ // if an error console.error(error);});
Nodejs
// router functionfunction something(req, res) { // console.log the request body console.log(req.body); // respond JSON object var _respond = { 'status': 200 }; // expressjs respond a JSON with status code 200 res.status(200).json(_respond);}// register route URLrouter.post('/api/url', something);
Note that the code end point URLs are same : /api/url
Therefore as your code sample in above question, you'r missing a /