easypost
Version:
Read POST data in node.js from a form submission or REST client using an easy manager method: easypost.get(req, res, function (data) {});
26 lines (23 loc) • 709 B
JavaScript
exports.get = function(req, res, callback) {
// Check if this is a form post or a stream post via REST client.
if (req.readable) {
// REST post.
var content = '';
req.on('data', function (data) {
if (content.length > 1e6) {
// Flood attack or faulty client, nuke request.
res.json({ error: 'Request entity too large.' }, 413);
}
// Append data.
content += data;
});
req.on('end', function () {
// Return the posted data.
callback(content);
});
}
else {
// Form post.
callback(req.body);
}
}