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?