0

I have created a server with node.js and express.js but if I send a POST request and try to check the body, it always says the body is empty. Here is my code:

app.js:

var express = require('express');

var app = express();

app.set('port', process.env.PORT || 61000);

require('./routes/routes.js')(app);

var server = app.listen(app.get('port'), function () {

    var port = server.address().port;

    console.log('Server runs on port %s', port);

});

routes/routes.js:

module.exports = function(app) {        

     app.get('/', function(req, res) { 
          res.status(200).json({"response": "Hello!"});    
     });     

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

         if (req.body != null) {
             res.status(200).json("Success");
         }

         res.status(200).json("Error");
     });
};

If I do a POST request now on www.hurl.it and add a body with a text like "Test", it gives me the response "Error", but it should give me the response "Success", because the body is not null.

And if I add the "body-parser" module to my app like this:

var bodyParser = require('body-parser');
...
app.use(bodyParser.json());

it gives me "Success" even if I let the body empty. If I return req.body the response is: {} and if I add a body it is also {}

Someone know what is wrong?

Nono
  • 1,073
  • 4
  • 23
  • 46

1 Answers1

0

I thinks its always hitting the error case. Try this:

if (req.body != null) {
  res.status(200).json("Success");
} else {
  res.status(200).json("Error");
}
Austin Miller
  • 34
  • 1
  • 4
  • Adding to this and empty hash `{}` is a valid body. So you might want to check that a hash is not empty. http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object – Austin Miller Oct 28 '16 at 14:10
  • Still not working. And why it gives me an empty hash {} if I have added something to the body? – Nono Oct 28 '16 at 14:19
  • I am sending the request with www.hurl.it/ and this is the location from my node server: [http://eis1617.lupus.uberspace.de/nodejs/](http://eis1617.lupus.uberspace.de/nodejs/) – Nono Oct 28 '16 at 14:33
  • Make sure you are adding them in the right order. AKA the body parser needs to be one of the first things added to the app: – Austin Miller Oct 28 '16 at 14:41
  • `var app = express(); var bodyParser = require('body-parser'); app.use(bodyParser.json()); var router = express.Router();` – Austin Miller Oct 28 '16 at 14:42
  • Yes I have it in that order. – Nono Oct 28 '16 at 14:48
  • Besides wrapping it in the if/else I am not sure. I am sorry I couldnt be of more help – Austin Miller Oct 28 '16 at 14:59
  • what is the client side call and how do u verify that you actually get a json msg 'on the wire' ? Depending on the client imple , if you OMIT the "Content-type" header or incorrectly have MME header instead of 'app-json' hdr for content-type, you may not be sending a json msg when u think that u r. Test client side with 'curl .... -d '{ your-json-here }' – Robert Rowntree Oct 28 '16 at 15:05
  • Another idea try this: `res.status(200).json(req.body);` That'll give you some good debug info – Austin Miller Oct 28 '16 at 15:07
  • res.status(200).json(req.body); this gives me: {} like I said. Even if I add a body it is {}. But thanks for your help. @Robert Rowntree, With Client you mean where I send the request from? Like I said before, I send it from www.hurl.it and I dont add any headers. – Nono Oct 28 '16 at 15:37
  • When I send the content type to `application/x-www-form-urlencoded` and pass it some json looking data i can get it to spit the body back out – Austin Miller Oct 28 '16 at 16:12
  • Well hower it works now when I add content-type application/json to the header. Thank you guys :) – Nono Oct 28 '16 at 16:17