How to access the request body when POSTing using Node.js and Express?
I have the following Node.js code:
var express = require('express');
var app = express.createServer(express.logger());
app.use(express.bodyParser());
app.post('/', function(request, response) {
response.write(request.body.user);
response.end();
});
Now if I POST something like:
curl -d user=Someone -H Accept:application/json --url http://localhost:5000
I get Someone
as expected. Now, what if I want to get the full request body? I tried doing response.write(request.body)
but Node.js throws an exception saying “first argument must be a string or Buffer” then goes to an “infinite loop” with an exception that says “Can’t set headers after they are sent.”; this also true even if I did var reqBody = request.body;
and then writing response.write(reqBody)
.
What’s the issue here?
Also, can I just get the raw request without using express.bodyParser()
?