I'm using the express.bodyParser() before the app.router, and the headers seem to be right, but I'm still getting undefined on req.body:
var app = express();
...
app.use(express.bodyParser());
...
app.use(app.router);
The output of req.headers is this:
{ host: '127.0.0.1:3000',
connection: 'keep-alive',
'content-length': '0',
'cache-control': 'max-age=0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
origin: 'http://127.0.0.1:3000',
'user-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
referer: 'http://127.0.0.1:3000/register',
'accept-encoding': 'gzip,deflate,sdch',
'accept-language': 'es-ES,es;q=0.8' }
And the post is declared like this:
app.post('/register/do', function(req, res) {
...
console.log(req.headers);
console.log(req.body);
...
});
What am I doing wrong?
The req. body object allows you to access data in a string or JSON object from the client side. You generally use the req. body object to receive data through POST and PUT requests in the Express server.
Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request.
The good news is that as of Express version 4.16+, their own body-parser implementation is now included in the default Express package so there is no need for you to download another dependency.
Next Steps. Express body-parser is an npm module used to process data sent in an HTTP request body. It provides four express middleware for parsing JSON, Text, URL-encoded, and raw data sets over an HTTP request body. Before the target controller receives an incoming request, these middleware routines handle it.
You got Content-Length: 0
, so the problem is on the client side. Your Express code looks OK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With