Is it possible to retrieve the body
contents using express
?
I started by trying body-parser
but that doesn't seem to work with GET
. Are there any modules which would work?
var express = require('express'),
bodyParser = require('body-parser'),
PORT = process.env.PORT || 4101,
app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.route('/')
.get(function(req, res) {
respond(req, res, 'GET body contents:\n');
})
.post(function(req, res) {
respond(req, res, 'POST body contents:\n');
});
app.listen(PORT, function(err) {
if (err) {
console.log('err on startup ' + err);
return;
}
console.log('Server listening on port ' + PORT);
});
/*
* Send a response back to client
*/
function respond(req, res, msg){
res.setHeader('Content-Type', 'text/plain');
res.write(msg);
res.end(JSON.stringify(req.body, null, 2));
}
This is response from GET
:
GET body contents:
{}
And from POST
:
POST body contents:
{
"gggg": ""
}